0

i have a question I need this select give me the nº of counts group by contenedor

SELECT C.Contenedor, count(DC.Contenedor) as NUM
FROM contenedores AS C, det_contenedores as DC
WHERE C.Contenedor=DC.Contenedor
GROUP BY C.Contenedor

But only appear the "C.Contenedor" that have "count(DC.Contenedor)" > 0

The result is (for example):

A->3
B->7
D->6

I would like to have:

A->3
B->7
C->0
D->6
E->0

any ideas? Thanks!!

Sergiooo87
  • 23
  • 8
  • Read this : http://stackoverflow.com/questions/14793057/how-to-include-zero-0-results-in-count-aggregate – kmas Nov 12 '15 at 10:32

1 Answers1

2

You need to use a LEFT JOIN instead of JOIN:

SELECT C.Contenedor, count(DC.Contenedor) as NUM
FROM 
    contenedores AS C 
    LEFT JOIN det_contenedores as DC
        ON C.Contenedor=DC.Contenedor
GROUP BY C.Contenedor
Arion
  • 31,011
  • 10
  • 70
  • 88
  • mmmm oki! but if i want to do a select that do the same but searching by one Contenedor?? Something like this `SELECT C.Contenedor, count(DC.Contenedor) as NUM FROM contenedores AS C LEFT JOIN det_contenedores as DC ON C.Contenedor=DC.Contenedor and C.Contenedor = 3 GROUP BY C.Contenedor` why dont appear CONTENEDOR 3--->0 (count) – Sergiooo87 Nov 12 '15 at 11:48