我試圖讓每個教室的學生人數在場。Union MYSQL中的GROUP BY
的條件是:
從教室裏所有的學生其中ClassroomID是< 99和具有(DropTime <> '00:00' 和PickupTime = '00:00' )和日期是20161111
從教室裏所有的學生都在那裏ClassroomId> 99,並有(DropTime = '00:00' )和日期是20161111
我使用日E採用在MySQL查詢並能正常工作,我得到正確的結果:
SELECT
COUNT(a.Id) AS total, c.Name
FROM
Attendance a
INNER JOIN
Classroom c ON a.classroom = c.Id
WHERE
DropDate = '20161111'
AND DropTime <> '00:00'
AND PickupTime = '00:00'
GROUP BY Name
不同之處在於,當我嘗試做一個工會獲得另外一間教室的結果(即ClassroomID> 99),它並沒有帶來各教室,只帶1個教室。 這是我正在使用的聯合查詢;
SELECT
SUM(total), Name
FROM
(SELECT
COUNT(a.Id) AS total, c.Name
FROM
Attendance a
INNER JOIN Classroom c ON a.classroom = c.Id
WHERE
DropDate = '20161111'
AND DropTime <> '00:00'
AND PickupTime = '00:00'
AND c.Id < 99
UNION ALL
SELECT
COUNT(a.Id) AS total, c.Name
FROM
Attendance a
INNER JOIN Classroom c ON a.classroom = c.Id
WHERE
DropDate = '20161111'
AND PickupTime = '00:00'
AND c.Id > 99) t
GROUP BY Name
完美謝謝! – snowflakes74