2013-02-22 217 views
0

我有mysql數據庫。如果查詢返回null,count = 0 mysql

我正在運行下面的查詢。

SELECT cluster, 
     infra_properties.property_name property, 
     count(*) 
FROM raw_alerts, 
     infra_properties 
WHERE infra_properties.parent_group = raw_alerts.cluster 
     AND cluster = 'abuse-content' 
     AND infra_properties.property_name = 'BigBro' 
     AND timestamp BETWEEN '2012-12-24 00:00:00' AND '2012-12-24 23:59:59' 
GROUP BY cluster; 

我得到的輸出爲空,但我的要求是得到計數= 0。如下所述。

+---------------+----------+----------+ 
| cluster  | property | count(*) | 
+---------------+----------+----------+ 
| abuse-content | BigBro | 0  | 
+---------------+----------+----------+ 
1 row in set (0.30 sec) 
+0

我跑了波紋管查詢,但仍然沒有運氣。選擇羣集,infra_properties.property_name屬性,COALESCE(COUNT(*),0)作爲從raw_alerts計數,infra_properties其中infra_properties.parent_group = raw_alerts.cluster和cluster ='abuse-content'和infra_properties.property_name ='BigBro'和時間戳之間'2013-1-24 00:00:00'和'2013-1-24 23:59:59'按羣組分組; – 2013-02-22 11:17:16

+0

你嘗試過'count(cluster)'嗎? – tttthomasssss 2013-02-22 11:22:39

+0

是的,已經試過了 – 2013-02-22 11:29:25

回答

0

如果2個表上沒有匹配的記錄,則不會返回任何行。

什麼你可能需要做的是有集羣和財產列額外的表,然後做一些事情,如: -

SELECT a.cluster, a.property, count(c.id) 
    FROM SomeExtraTable a 
    LEFT OUTER JOIN raw_alerts b ON a.cluster = b.cluster AND AND b.cluster = 'abuse-content' 
    LEFT OUTER JOIN infra_properties c 
ON a.property = c.property 
AND c.parent_group = b.cluster 
AND c.property_name = 'BigBro' 
AND c.timestamp BETWEEN '2012-12-24 00:00:00' AND '2012-12-24 23:59:59' 
    GROUP BY a.cluster, a.property 

(我只是在猜測時間戳列是什麼表)