I have 2 tables:
-
The first contains data for the current day (per hour) – this table has raw data, that needs to be processed.
-
The second contains data for previous day (per day) – this table already has the values calculated for every day.
I want to combine this to rows in a MySQL query, so that I return the data for previous dates (per day) and for current day (again, per day).
Currently I am using 2 mysql queries:
//table 1, data for current day
$qry1="
select DATE_FORMAT(completedate,'%Y-%m-%d') as date, SUM(IF(complete = 1,affpayout,0)) as pay, COUNT(*) as leads
from leads
where affiliateid={$_SESSION["id"]} AND completedate>'$date'
GROUP BY DATE_FORMAT(completedate,'%Y-%m-%d')";
//table 2, data for previous days, already processsed, we just need to select it
$qry2="
select DATE(date) as date, affrevenue as pay, totalleads as leads
from leadsdays
GROUP BY DATE(date)";
How can I combine the 2 efficiently (speed performance is an issue)? What would be the best way to do this?
Try to
UNION. Also, make sure your tables are indexed properly. Looks like you’ll wantcompletedateandaffiliateidindexed at least. Not sure how much more efficient two queries is versus one union though…