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

输入图片说明

相关文章
相关标签/搜索