Reputation: 4662
$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';
Is there a way to combine these two queries. I really need them in one resource.
currently each one is showing number of number request per day. I want to combine them so that each date
has phone_TotalPerDay
and fax_TotalPerDay
.
Upvotes: 0
Views: 74
Reputation: 1036
You could use a conditional sum, something like
$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';
The if
statements return 1 if the request type matches and 0 otherwise, so summing these effectively counts the rows matching this condition, and I've removed the request criteria from the where clause.
Upvotes: 1