我有一个简单的查询要获取,但是我被卡住了,我真的不知道怎么做。
更具体地说:
Allarmi有很多条目,我将它们按Allarmi.IDComponente分组显示
像这样的东西

SELECT *
FROM ALLARMI AS ALLARMI
WHERE ID_CELLA = 1
GROUP BY ID_Componente

我只需要将aCOUNT列添加到这个表中,比如
(SELECT COUNT( * )
FROM allarmi
WHERE isAttivo = 1
GROUP BY id_componente)

但我想显示COUNT即使它0NULL
我该怎么做?
提前谢谢

最佳答案

假设tableComponenti定义了组件,并且您希望从allarmi中获取每个组件的记录计数,那么您实际上需要做的是从Componenti中选择组件并在allarmi上添加LEFT OUTER JOIN

SELECT COMPONENTI.ID_Componente, COUNT(ALLARMI.ID_Componente)
FROM COMPONENTI
LEFT OUTER JOIN ALLARMI ON
   -- insert your join here!
   -- maybe COMPONENTI.ID_Componente= ALLARMI.ID_Componente?
   -- add any query for this table here as well.
   AND ALLARMI.ID_Cella = 1
GROUP BY COMPONENTI.ID_Componente

关于mysql - 卡在一个简单的查询上-OUTER JOIN,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15504110/

10-16 01:37