select COUNT(*) from spu_goods where category_id=333 and ext1='no'
结果为:
count(*) |
0 |
但是,在加上group by 之后,如:
select COUNT(*) from spu_goods where category_id=333 and ext1='no' group by shop_id
结果为:
count(*) |
null |
之所以,没有返回0,是因为“group by shop_id” ,在对结果进行分组之后,如果为空,则不返回0,而是返回null
解决办法:ifnull(expr1,expr2)函数,若expr1为null,则返回expr2
select IFNULL(count(*),0) from (select COUNT(*) from spu_goods where category_id=333 and ext1='no' group by shop_idifnull()
结果为:
ifnull(count(*),0) |
0 |