2017-03-12 5 views
0

に参加しSQLグループは、これらは私のテーブルです

select count(*), entity_type from entities group by entity_type; 

私は明らかにこのようなものを得ます:

+----------+---------------+ 
| count(*) | entity_type | 
+----------+---------------+ 
| 15418 | locations  | 
| 21789 | misc   | 
| 62306 | organizations | 
| 121307 | people  | 
+----------+---------------+ 

は、私が欲しいのはこのようなものです

+----------+---------------+------+ 
| count(*) | entity_type | site | 
+----------+---------------+------+ 
| 15418 | locations  | ABC | 
| 21789 | misc   | ABC | 
| 62306 | organizations | ABC | 
| 121307 | people  | ABC | 
| 13418 | locations  | CNN | 
| 22789 | misc   | CNN | 
| 65306 | organizations | CNN | 
| 132307 | people  | CNN | 
+----------+---------------+------+ 

このような種類のカウントを提供するには、どのようにクエリを設定できますか?

+0

ヒント: 'JOIN'。そして再びJOINする。 –

+0

...あなたの問題は何ですか?あなたは一緒に3つのテーブルを結合しようとしましたか? – Bakuriu

答えて

1

あなたはjoinが必要になります:

select count(*), entity_type, site 
from entities 
left join articles_entities on entity_id = entities.id 
left join articles on article_id = articles.id 
group by entity_type, site; 

私はあなたが結果に含またい記事に対応していないエンティティを有することができることを前提にleft joinを使用。そうでない場合はleftを削除できます。

関連する問題