2012-08-25 180 views
0
$query1 = 'select DATE_FORMAT(time, "%Y-%m-%d") as date, 
     count(*) as phone_TotalPerDay 
     from numrequest 
     where id_usr = "'.$id_user.'" 
     AND id_re ="'.$id_re.'" 
     AND request="ph" 
     group by id_usr, year(time), DAYOFYEAR(time) 
     order by year(time) DESC, DAYOFYEAR(time) DESC'; 


$query2 = 'select DATE_FORMAT(time, "%Y-%m-%d") as date, 
     count(*) as fax_TotalPerDay 
     from numrequest 
     where id_usr = "'.$id_user.'" 
     AND id_re ="'.$id_re.'" 
     AND request="fx" 
     group by id_usr, year(time), DAYOFYEAR(time) 
     order by year(time) DESC, DAYOFYEAR(time) DESC'; 

有没有办法组合这两个查询。我真的需要他们在一个资源。 当前每个人都显示号码请求每天。我想合并它们以便每个date具有phone_TotalPerDayfax_TotalPerDay结合两个MySQL查询?

回答

1

你可以使用条件的总和,像

$query1 = 'select DATE_FORMAT(time, "%Y-%m-%d") as date, 
    sum(if(request = "ph", 1, 0)) phone_TotalPerDay, 
    sum(if(request = "fx", 1, 0)) fax_TotalPerDay 
    from numrequest 
    where id_usr = "'.$id_user.'" 
    AND id_re ="'.$id_re.'" 
    group by id_usr, year(time), DAYOFYEAR(time) 
    order by year(time) DESC, DAYOFYEAR(time) DESC'; 

东西的if语句返回1,如果该请求类型匹配,否则为0,所以总结这些有效计数匹配这个条件的行,我已经删除来自where子句的请求标准。

+0

这是工作:)。非常感谢。 –