1

我有一个简单的查询要获得,但我被卡住了,我真的不知道该怎么做。更具体地说:该表Allarmi有很多条目,我将它们全部分组显示Allarmi.IDComponente

像这样的东西

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

我只需要在此表中添加一COUNT列,例如

(SELECT COUNT( * ) 
FROM allarmi
WHERE isAttivo = 1
GROUP BY id_componente)

但我想展示COUNT即使它0NULL

我该怎么做?提前致谢

4

1 回答 1

0

假设表Componenti定义了组件,并且您想从allarmi每个组件中获取记录计数,您实际想要做的是从中选择组件并Componenti添加一个LEFT OUTER JOINallarmi

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
于 2013-03-19T16:01:44.937 回答