0
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

我仍然无法按地区将它们分组在一起。

4

3 回答 3

1

我希望事件列表按区域分组——现在不管它们是按什么排序的。如果它们按 regs 排序——我会将来自波士顿的所有事件聚集在一起显示,然后按 regs 排序。

您需要 ORDER BY,而不是 GROUP BY。您可以在 ORDER BY 子句中的所有其他字段之前添加要“聚集在一起”的字段。

ORDER BY region_id, regs
于 2012-05-10T06:41:36.793 回答
0

Here is a modified version

SELECT
  events.id,
  events.region_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

Try this i have only selected the column events.region_id

于 2012-05-10T06:43:28.920 回答
0

If you want to "clumped together" then it sound like you want to use and aggregate for the regs.

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, 
  SUM(r.regs) AS regs
....
GROUP BY events.region_id, events.id ORDER BY date_1 DESC LIMIT 0, 50
于 2012-05-10T06:43:42.527 回答