mysql:condition和groupby一些零值的行

时间:2017-08-03 08:15:23

标签: mysql

以下是我的查询我想显示商店中的所有商品,即使商品数量为零

select count(itemn) as count, item from mytable group by item
This lists

count      item
 234        1
  45        2
  65        3
  23        4

但是当我应用某些条件时,它没有显示所有项目

select count(itemn) as count, item from mytable where price <230 group by item

count      item
 234        1
  23        4

但我希望结果如

count     item
 234       1
   0       2
   0       3
  23       4

1 个答案:

答案 0 :(得分:0)

然后尝试这样做:

select SUM(case 
    when price<230 then 1  
    else 0 
    end) as count, item 
from mytable 
group by item