Mysql內置函數理解

以下可以使用SELECT就使用SELECT

 

隨機返回浮點數

select  rand(N)  返回0-1之間的浮點數

 

四捨五入

select round(X, D)   四捨五入保留D爲小數,X可以是表具體字段,  D默認爲0,可以爲負數,如round(19, -1)返回20

select round(unit_price, 0) unit_price from test; 

返回鏈接新的字符串

select concat(type1, type2, ...) mm from test (返回鏈接參數產生的字符串 )


select CONCAT_WS('-',unit_price_type,contract_type)dk from test  (使用連接符 - 鏈接其他參數產生的字符串)


 

條件判斷語句

IF(expr1, expr2, expr3): 如果expre1不爲0或者null,則返回expr2的值,否則返回expr3的值

select if(unit_price, unit_price, 1000) unit_price from test; 




IFNULL(expr1, expr2): 如果expr1不爲null,返回expr1,否則返回expr2

select ifnull(contract_type, 0) contract_type from test;


MULLIF(expr1, expr2): 如果expr1 = expr2則返回null, 否則返回expr2

select nullif(contract_type, unit_price_type) istrue from  test;


CASE value WHEN [compare_value] THEN result [WHEN [compare_value] THEN result ...] [ELSE result] END     當compare_value = value 的時候返回result

select case unit_price WHEN 100 THEN '符合' WHEN 200 THEN '不符合' ELSE '廢棄' END 是否符合 from test;




CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END
當condition爲TRUE時返回result

select CASE WHEN unit_price_type = contract_type THEN '是' ELSE '否' END 是否 from test;




 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章