SELECT b.PlateNumber, BusModel, Count(DISTINCT d.StaffID) AS NumberOfDirvers
FROM Bus b, Trip t, Driver d, Staff s
WHERE b.PlateNumber = t.PlateNumber
AND t.StaffID = d.StaffID
AND d.StaffID = s.StaffID
AND b.capacity = 72
AND EXTRACT(MONTH FROM s.DateOfBirth) BETWEEN '07' AND '12'
GROUP BY PlateNumber, BusModel;
显然有一些公共汽车还没有与任何司机旅行,但我的代码似乎无法显示驱动器的数量为0.如何显示它?
问题:
给定总线容量,比如说72,找到具有指定容量的总线容量。
对于集合中的 EVERY 总线,列出:
答案 0 :(得分:1)
使用IFNULL(value, 0)
显示0
未找到任何驱动程序。您也可以使用JOINS
SELECT b.PlateNumber, BusModel, IFNULL(Count(DISTINCT d.StaffID), 0) AS NumberOfDirvers
FROM Bus b LEFT JOIN Trip t ON b.PlateNumber = t.PlateNumber
LEFT JOIN Driver d ON t.StaffID = d.StaffID
LEFT JOIN Staff s d.StaffID = s.StaffID
WHERE EXTRACT(MONTH FROM s.DateOfBirth) BETWEEN '07' AND '12'
GROUP BY b.PlateNumber, BusModel;