SELECT events.id, events.name, events.code, event_statuses.name as event_status_name
, regions.name as event_region_name, event_locations.name as event_location_name
, events.date_1, events.date_2
, count(r.id) as regs
FROM events
INNER JOIN event_locations ON event_locations.id = events.event_location_id
INNER JOIN event_statuses ON event_statuses.id = events.event_status_id
LEFT OUTER JOIN regions ON regions.id = events.region_id
LEFT OUTER JOIN registrations as r ON events.id = r.event_id and r.registration_status_id = 4
WHERE 1 GROUP BY events.id ORDER BY date_1 DESC LIMIT 0, 50
正如你所看到的,我的查詢有一個聚合函數,它在按事件分組的註冊上執行。查詢中的兩個分組,其中一個沒有聚合函數。
不過,我想有另一GROUP BY
在此查詢,這其中沒有任何其他聚集函數。這裏的要點是,我希望事件列表按地區分組 - 現在他們按照他們排序。如果他們按regs
排序 - 我會把來自波士頓的所有活動聚集在一起,然後按區域排序。因此需要另一個Group By。這是我想出了,但這是行不通的:
SELECT events.id, events.name, events.code, event_statuses.name as event_status_name
,regions.name as event_region_name, event_locations.name as event_location_name
, events.date_1, events.date_2, r.regs
FROM events
INNER JOIN event_locations ON event_locations.id = events.event_location_id
INNER JOIN event_statuses ON event_statuses.id = events.event_status_id
LEFT OUTER JOIN regions ON regions.id = events.region_id
LEFT OUTER JOIN
(Select event_id, count(id) as regs
from registrations where registration_status_id = 4
group by event_id) r on events.id = r.event_id
WHERE 1 GROUP BY events.region_id, events.id ORDER BY date_1 DESC LIMIT 0, 50
我仍然不能將它們按地區分組在一起。
什麼不起作用? – Arion