我有下表
TABLE 存储
store name level
1 Tom 4
2 Joe 2
1 Chris 4
3 Tom 2
4 Ed 2
2 Tom 4
3 Chris 2
我想返回每个不同商店的 4 级数量 我知道我可以
select distinct store from store;
获得不同的商店和
select count(*) as level from store where level = 4;
获取第 4 级的数量
我如何组合以返回每个不同商店中 4 级数量的查询 所以上面的数据会返回
store level4
1 2
2 1
3 0
4 0
最佳答案
不清楚为什么你的表被称为store
。难道你不应该有一个同名的表,每个商店一行?
无论如何,获取 0
计数的最简单方法可能是条件聚合:
select store, sum(level = 4) as level4
from store
group by store;
关于mysql - 如何形成这个 MYSQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57101101/