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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top