我有三个表,我想用另一个表列中的组来计数每一行。
问题是,当没有找到记录时,组计数将不会返回任何内容。
因此,我希望为每个未找到记录的组添加空值。
以下是查询:
select monster.monster_name,count(*) as count
from monster right join monster_ability
on monster.monster_id= monster_ability.monster_id where
isnull(monster_ability.used)
group by monster.monster_id这是小提琴:小提琴
我希望结果应该是这样的:
| monster_name | count |
|--------------|-------|
| kora | 1 |
| lowdowu | 3 |
| ngjengeh| null|
| lortyu | 1 |
| foh du fy| null|发布于 2017-03-15 08:24:50
当计数为0时,使用case when获得null:
select
m.monster_name,
case when count(a.ability_id) = 0 then null else count(a.ability_id) end as `count`
from monster m
left join monster_ability ma on m.monster_id = ma.monster_id and ma.used is null
left join ability a on ma.ability_id = a.ability_id
group by m.monster_id我是SQLFiddle 演示。
https://stackoverflow.com/questions/42804263
复制相似问题