mysql 的一些條件查詢語句

輸入圖片說明

一、當查詢表中某個字段時,當字段值出現是""或null時,想設置成默認值的方法以下語句:當字段value 爲「」或null時,默認爲0.函數

select name ,case value WHEN \"\" THEN 0  WHEN NULL THEN 0 ELSE `value` end as value from zy_home_page

二、根據group by 求比用ROUND(sum(列1)/sum(列2), 4)函數. 列2 爲0時返回null ,4表示保留小數點後多少位。如code

SELECT dept,home,role ,sum(source),ROUND(sum(source)/sum(source), 4) as order_rate from test_group_by GROUP BY dept,home

輸入圖片說明

三、若是ROUND(sum(source)/0, 4)結果爲null ,能夠用ifnull(expr1,expr2)判斷。假如expr1不爲NULL,則IFNULL()的返回值爲expr1; 不然其返回值爲expr2。圖片

SELECT dept,home,role ,sum(source),ifnull(ROUND(sum(source)/0, 4),0) as order_rate  from test_group_by GROUP BY dept,home

輸入圖片說明

相關文章
相關標籤/搜索