2010-08-20 43 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永远不会为空。