2011-12-11 12 views
0

私は自分のDBにある都市の合計を返すクエリを持っています。MySQL concat UNION GROUP BY行の結果

私は次のことを紹介しています。1人の人が自分の住所を持つことができますが、自分の住所に関連付けることもできます。そして、私はその人がいる都市の総計を返したい。

私はすべての後

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 

[OK]をクエリが、私は理解して少し複雑です。私は次のように受け取りたい

+---------+---------+-----------+ 
| 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 | 
+---------+---------+-----------+ 

がどのように私は私に所望の値を実行するために、私のクエリを書くことができます。しかし、私の問題は、それは私が希望のようagroupedていない結果です。 ありがとうございました!

+0

私に理由を説明してくださいdownvoteするには。 – workdreamer

+1

アドレステーブルの都市を数えてみませんか? //私はdownvoteをしなかった – ncank

+0

@ i-joey私はエンティティをフィルタリングしているので。 – workdreamer

答えて

2

あなたは、単につまり、これら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: 'UNION'サブクエリの' LIMIT'も外部クエリに移動します。 –

+0

結果:最初と2番目のクエリでグループを保存する必要がありました。 これは非常に巧妙な解決策です。ありがとうございました。 – workdreamer