I have the tables below:
- articles
- tags
- article_tag (pivot table with article_id and tag_id columns)
In the query below I can retrieve articles that have both tags (with id 1 and 3). In the query only articles that have the two tags that will have their ids shown.
select article_id
from article_tag
where tag_id in (1, 3)
group by article_id
having count(distinct tag_id) = 2;
How to do a select count so I know the total of returned records without displaying the data itself?
Is that in the above query I have the listing for example:
| article_id |
--------------
| 289 |
| 341 |
But I wanted something like (total articles that have both tags):
| total_articles |
------------------
| 2 |