2012-05-10 74 views
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排序 - 我會把來自波士頓的所有活動聚集在一起,然後按區域排序。因此需要另一個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 

我仍然不能將它們按地區分組在一起。

+0

什麼不起作用? – Arion

回答

1

我想事件列表按地區分組 - 現在他們按照他們排序。如果他們按照regs排序 - 我會把來自波士頓的所有活動聚集在一起,然後按照regs排序。

你想ORDER BY這一點,不是GROUP BY。你可以在你的ORDER BY子句的所有其他字段之前添加你想要「聚集在一起」的字段。

ORDER BY region_id, regs 
0

這裏是一個修改版本

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 

試試這個,我只選擇列events.region_id

+0

你有什麼修改? –

+0

將被然後選擇我已經添加events.region_id將 –

0

如果你想「聚集在一起」,那麼它聽起來像你想使用並彙總爲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 
+0

用於羣我使用的英語不好阿里昂對不起,我剛纔只是說分。 –

相關問題