2010-08-20 107 views
0

第一行是我的結果全部爲空,我如何從結果中刪除該結果?MYSQL:我如何刪除第一行?

這是我的查詢:

SELECT COALESCE(UNIX_TIMESTAMP(Date(p.published_at)),0) as 'day', 
     COALESCE(SUM(case when p.status = 2 then p.value end),0) as 'total_accepted', 
     COALESCE(SUM(case when p.status = 1 then p.value end),0) as 'total_open', 
     COALESCE(SUM(case when p.status = 3 then p.value end),0) as 'total_declined', 
     COALESCE(SUM(case when p.status !=0 then p.value end),0) as 'total_published' 
FROM posts as p 
GROUP BY DATE(p.published_at); 

伊夫用於聚結從我結果的其餘部分刪除任何空值,所以第一行是技術上全部爲0了。但我這個繪圖數據,以及我行開始他們都在1970年...和計算機不存在當年= P

回答

1

從您的問題描述,這應該解決這個問題:

SELECT COALESCE(UNIX_TIMESTAMP(Date(p.published_at)),0) as 'day', 
     COALESCE(SUM(case when p.status = 2 then p.value end),0) as 'total_accepted', 
     COALESCE(SUM(case when p.status = 1 then p.value end),0) as 'total_open', 
     COALESCE(SUM(case when p.status = 3 then p.value end),0) as 'total_declined', 
     COALESCE(SUM(case when p.status !=0 then p.value end),0) as 'total_published' 
FROM posts as p 
WHERE p.published_at IS NOT NULL 
GROUP BY DATE(p.published_at); 
0
SELECT COALESCE(UNIX_TIMESTAMP(Date(p.published_at)),0) as 'day', 
     COALESCE(SUM(case when p.status = 2 then p.value end),0) as 'total_accepted', 
     COALESCE(SUM(case when p.status = 1 then p.value end),0) as 'total_open', 
     COALESCE(SUM(case when p.status = 3 then p.value end),0) as 'total_declined', 
     COALESCE(SUM(case when p.status !=0 then p.value end),0) as 'total_published' 
FROM posts as p 
WHERE 'day' <> 0 
GROUP BY DATE(p.published_at) 

就過濾掉不需要的行。另一種方式來做到這將是

SELECT COALESCE(UNIX_TIMESTAMP(Date(p.published_at)),0) as 'day', 
     COALESCE(SUM(case when p.status = 2 then p.value end),0) as 'total_accepted', 
     COALESCE(SUM(case when p.status = 1 then p.value end),0) as 'total_open', 
     COALESCE(SUM(case when p.status = 3 then p.value end),0) as 'total_declined', 
     COALESCE(SUM(case when p.status !=0 then p.value end),0) as 'total_published' 
FROM posts as p 
WHERE p.published_at is not null 
GROUP BY DATE(p.published_at) 
+0

如果不止一個記錄具有零作爲'day'價值是什麼? – 2010-08-20 17:15:30

+0

這應該不重要,因爲它們在「GROUP BY」看到它們之前被過濾掉了。 – Frank 2010-08-20 17:23:43

0

您可以通過使用WHERE子句過濾掉包含所有空值行:

WHERE p.published_at IS NOT NULL AND p.status IS NOT NULL and p.value IS NOT NULL 

添加此到查詢給

SELECT COALESCE(UNIX_TIMESTAMP(Date(p.published_at)),0) as 'day', 
     COALESCE(SUM(case when p.status = 2 then p.value end),0) as 'total_accepted', 
     COALESCE(SUM(case when p.status = 1 then p.value end),0) as 'total_open', 
     COALESCE(SUM(case when p.status = 3 then p.value end),0) as 'total_declined', 
     COALESCE(SUM(case when p.status !=0 then p.value end),0) as 'total_published' 
FROM posts as p 
    WHERE p.published_at IS NOT NULL AND p.status IS NOT NULL and p.value IS NOT NULL 
GROUP BY DATE(p.published_at); 

如果它是隻需足以過濾NULL日期,然後使用

WHERE p.published_at IS NOT NULL 

是你所需要的。一旦WHERE子句就位,日期上就不需要COALESCE,因爲publish_at永遠不會爲空。