Calculate number of fields
-
28-06-2021 - |
题
I have three tables:
- Article(idArticle,NameArt)
- Tag(idTag, NameTag)
- ArtiTag(idArticle,idTag)
I want to have a result like this: NameTag,Count(Articles that belongs to that tag)
I tried the following:
SELECT Tag.NameTag , COUNT(DISTINCT(idArticle))
FROM ArtiTag, ArtiTag
but it returns always only one row, even if I have many tags and many articles related
解决方案
SELECT t.NameTag, COUNT(*)
FROM ArtiTag at
INNER JOIN Tag t
ON at.idTag = t.idTag
GROUP BY t.NameTag;
其他提示
Select T.idTag, Max(nametag), count(artitag.idArticle) from Tag t
left join ArtiTag on t.idTag=ArtiTag.idTag
Group by t.idTag
This query outputs all tags including also tags with 0 articles.
不隶属于 StackOverflow