我有两张桌子career_types和预订
career_types
id | name
1 | foo
2 | bar
3 | baz预订
id | career_type_id | details
1 | 1 | foo
2 | 1 | bar
3 | 2 | baz我的预订表中有很多关于所有职业类型的条目,除了career_types.id of 3。
我需要一个查询,该查询将列出我的职业类型名称和每次预订的数量,在没有预订的情况下,career_types.id =1。我想要返回0。
到目前为止我一直在尝试各种变体
SELECT career_types.name, sum(bookings.id)
FROM career_types
LEFT JOIN bookings ON (career_types.id = bookings.career_type_id)
GROUP BY career_types.id我想要的结果是
career_types.name | sum
foo | 2
bar | 1
baz | 0但是目前我还不能得到任何关于baz的产出。
发布于 2018-09-04 21:44:43
聚合函数在计算中只包含非空值。对于SUM和大多数聚合函数,如果没有遇到非空值,则结果为null。如果需要零,而不是null,这可以简单地用:IFNULL(SUM(x), 0)来解释。
https://stackoverflow.com/questions/52174105
复制相似问题