来自mysql查询的零值结果数组

时间:2015-02-28 15:21:12

标签: php mysql

我有以下mysql查询

select registration, 
sum(`week01`) as `2015/01`, 
sum(`week02`) as `2015/02`
from (select registration, 
case when concat(YEAR(act_del_time_arrive),'/',week(act_del_time_arrive))='2015/01' then round(costed_amount,0) else '' end 'week01' , 
case when concat(YEAR(act_del_time_arrive),'/',week(act_del_time_arrive))='2015/02' then round(costed_amount,0) else '' end 'week02'
from vw_tekwani_schedule_main
                       where act_del_time_arrive between '2015-01-04' and '2015-02-28'
                       and haulier_id = 4 and registration is not null
                       group by registration, date(act_del_time_arrive)
                       order by registration) as t group by registration

产生以下结果

reg   2015/01 2015/02
'A10'  , '0'  , '0'
'A2'   , '0'  , '0'
'A3'   , '0'  , '0'
'A4'   , '0'  , '0'
'A5'   , '0'  , '0'
'A6'   , '0'  , '0'
'A7'   , '0'  , '0'
'A8'   , '0'  , '0'
'A9'   , '0'  , '0'

我迫切需要了解如何过滤2015/01和2015/02年度零值的行。

所以理论上我不会从这个查询中返回结果

1 个答案:

答案 0 :(得分:1)

只需使用having子句:http://dev.mysql.com/doc/refman/5.0/en/group-by-handling.html

select registration, 
sum(`week01`) as `2015/01`, 
sum(`week02`) as `2015/02`
from (select registration, 
case when concat(YEAR(act_del_time_arrive),'/',week(act_del_time_arrive))='2015/01' then round(costed_amount,0) else '' end 'week01' , 
case when concat(YEAR(act_del_time_arrive),'/',week(act_del_time_arrive))='2015/02' then round(costed_amount,0) else '' end 'week02'
from vw_tekwani_schedule_main
                       where act_del_time_arrive between '2015-01-04' and '2015-02-28'
                       and haulier_id = 4 and registration is not null
                       group by registration, date(act_del_time_arrive)
                       order by registration) as t group by registration
HAVING sum(`week01`) > 0 OR sum(`week02`) > 0
相关问题