Calcular número de campos

Tengo tres mesas:

  • 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)

He probado lo siguiente:

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

preguntado el 24 de agosto de 12 a las 20:08

2 Respuestas

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

Respondido 24 ago 12, 20:08

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.

Respondido 24 ago 12, 20:08

Thanks; The following fix the case where some tags have 0 articles Select Distinct(ArtiTag.idTag), nametag, count(artitag.idArticle) from Tag t left join ArtiTag on t.idTag=ArtiTag.idTag WHERE ArtiTag.idTag IS NOT NULL Group By t.idTag; - Oumdaa

Select NameTag, COUNT(ArtiTag.idArticle) from Tag t left join ArtiTag on t.idTag=ArtiTag.idTag WHERE ArtiTag.idTag IS NOT NULL Group By t.NameTag - Oumdaa

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.