I’ve two tables, Table1(News) and Table2(Subscribers).
Table1: id, news_title
Table2: id, news_id, user_id
Table1
id | news_title
--------------
1 | News 1
2 | News 2
3 | news 3
Table2
id | news_title_id | user_id
----------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 3
I need a query result like this.
news_title | subscribers
------------------------
News 1 | 2
News 2 | 1
News 3 | 0
Any help?
A simple aggregate
COUNT(*)with aLEFT JOINwill do the job here.LEFT JOINandCOUNT(*)(rather thanCOUNT(user_id)) are used to be sure titles with zero subscribers still return a row.