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
- 我会将来自波士顿的所有事件聚集在一起显示,然后按 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
我仍然无法按地区将它们分组在一起。