这是我的表:
我正在运行此查询,
SELECT fruits,COUNT(*) as count FROM meva where owner = 'bill' GROUP BY fruits ORDER BY count
我的结果是
如果没有像这样的所有者匹配,是否有可能将其他水果项目设为0,
Fruit | count
Apple , 2
Mango , 0
Banana , 0
Strawberry ,0
Appricot, 0
Alfonso , 0
我非常感谢我的查询中的一点修改。 感谢
答案 0 :(得分:1)
使用SUM()
和CASE
SELECT fruits,
SUM(CASE WHEN owner = 'bill' THEN 1 ELSE 0 END) as `count `
FROM meva
GROUP BY fruits
ORDER BY fruits
或使用IF
(仅在MySQL
)
SELECT fruits,
SUM(IF(owner = 'bill',1,0)) as `count `
FROM meva
GROUP BY fruits
ORDER BY fruits
答案 1 :(得分:1)
是的,那里有一个很好的答案。你也可以试试这个!
SELECT fruits,
SUM(Coalesce(owner='bill',0)) as counts
FROM meva
GROUP BY fruits
ORDER BY fruits
;