2011-12-11 158 views
0

我有一個查詢來返回我在我的數據庫上的城市總數。MySQL concat UNION GROUP BY行結果

好吧,讓我介紹一下:一個人可以有他的地址,但他也可以有與他的工作地址相關的地址。而且我想要返回人員所在城市的總數。

假如我有

Pablo living in Lisbon but working in Porto 
Jim living in Paris and working in Paris 
May living in Lisbon and working in Paris 

I have i should have a result like: 
Paris - 3 
Lisbon - 2 
Porto - 1 

畢竟,我的查詢是:

SELECT 
    entity_address.city as name, 
    entity_address.city as id, 
    COUNT(entity_address.city) as count_all 
FROM `entities` LEFT JOIN enterprises_entities ON entities.id = enterprises_entities.entity_id 
LEFT JOIN entities AS `enterprises` ON enterprises.id = enterprises_entities.enterprise_id 
LEFT JOIN addresses as enterprise_address ON enterprise_address.id = enterprises_entities.address_id 
LEFT JOIN addresses as entity_address ON entity_address.entity_id = entities.id 
LEFT JOIN person_titles ON person_titles.id = entities.title_id AND entities.title_type = 'PersonTitle' 
LEFT JOIN enterprise_activities ON enterprise_activities.id = enterprises.title_id AND enterprises.title_type = 'EnterpriseActivity' 
LEFT JOIN positions ON entities.position_id = positions.id 

WHERE enterprise_address.city != '' OR entity_address.city != '' 
GROUP BY name 

UNION 

SELECT 
    enterprise_address.city as name, 
    enterprise_address.city as id, 
    COUNT(enterprise_address.city) as count_all 
FROM `entities` LEFT JOIN enterprises_entities ON entities.id = enterprises_entities.entity_id 
LEFT JOIN entities AS `enterprises` ON enterprises.id = enterprises_entities.enterprise_id 
LEFT JOIN addresses as enterprise_address ON enterprise_address.id = enterprises_entities.address_id 
LEFT JOIN addresses as entity_address ON entity_address.entity_id = entities.id 
LEFT JOIN person_titles ON person_titles.id = entities.title_id AND entities.title_type = 'PersonTitle' 
LEFT JOIN enterprise_activities ON enterprise_activities.id = enterprises.title_id AND enterprises.title_type = 'EnterpriseActivity' 
LEFT JOIN positions ON entities.position_id = positions.id 

WHERE enterprise_address.city != '' OR entity_address.city != '' 
GROUP BY name 

order by count_all DESC 

LIMIT 5 

好的查詢是有點複雜的我明白了。但我的問題是,結果它不是agrouped像我將:

+---------+---------+-----------+ 
| name | id  | count_all | 
+---------+---------+-----------+ 
| Lisbon | Lisbon |  5100 | 
+---------+---------+-----------+ 
| Lisbon | Lisbon |  932 | 
+---------+---------+-----------+ 
| Paris | Paris |  430 | 
+---------+---------+-----------+ 
| Porto | Porto |  270 | 
+---------+---------+-----------+ 
| Paris | Paris |  92 | 
+---------+---------+-----------+ 

我想收到這樣的:

+---------+---------+-----------+ 
| name | id  | count_all | 
+---------+---------+-----------+ 
| Lisbon | Lisbon |  6032 | 
+---------+---------+-----------+ 
| Paris | Paris |  512 | 
+---------+---------+-----------+ 
| Porto | Porto |  270 | 
+---------+---------+-----------+ 
| London | London |  80 | 
+---------+---------+-----------+ 
| Berlin | Berlin |  10 | 
+---------+---------+-----------+ 

我怎麼能寫我的查詢來執行我所期望的值。 謝謝!

+0

要downvote請解釋我的原因。 – workdreamer

+1

你爲什麼不計算地址表中的城市? //我沒有downvote – ncank

+0

@ i-joey因爲我正在過濾實體。並且我需要結果 – workdreamer

回答

2

你只需要從這兩個查詢SUM聚合值,即:

SELECT t.id, SUM(t.count_all) 
FROM (
    SELECT entity_address.city as id, COUNT(entity_address.city) as count_all 
    FROM /* rest of your first query */ 
    UNION 
    SELECT enterprise_address.city as id, COUNT(enterprise_address.city) as count_all 
    FROM /* rest of your second query */ 
) t 
GROUP BY t.id 
+0

@workdreamer:同時將任何'LIMIT'移出UNION子查詢到外部查詢中。 –

+0

結果:我必須通過第一個和第二個查詢來保留組。 這是一個非常聰明的解決方案。謝謝。 – workdreamer