Pergunta

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

Foi útil?

Solução

SELECT t.NameTag, COUNT(*)
    FROM ArtiTag at
        INNER JOIN Tag t
            ON at.idTag = t.idTag
    GROUP BY t.NameTag;

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top