MYSQL:如何删除第一行?

时间:2010-08-20 17:08:26

标签: sql 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);

我已经使用coalesce从我的结果的其余部分中删除任何空值,所以第一行在技术上现在都是0。但是我正在绘制这些数据,我的线路始于1970年...当时计算机不存在= p

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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)

答案 2 :(得分:0)

您可以使用where子句过滤掉包含所有NULL的行:

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,因为published_at永远不会为空。