我有个问题
我需要这个选择,让我按竞争者的数量分组

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


但仅显示“ count(DC.Contenedor)”> 0的“ C.Contenedor”

结果是(例如):

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


我想拥有:

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


有任何想法吗?谢谢!!

最佳答案

您需要使用LEFT JOIN而不是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

关于mysql - 计数为0的结果未出现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33669290/

10-10 05:11