Reputation: 81
I'm having a table that contains 6 columns: id (PK, A_I), systemID (varchar(32)), total (varchar(32)), difference (varchar(16)), updated (datetime), error (int). The table is updated every hour and right now there are over 2 million records. Therefore I want to split the table up into one containing just todays values and one containing historical data. Every day at midnight data from todays table is moved to historical. So far so good.
Problem is that the user has the option to view the difference values in day view, week view, month view and year view. I don't know how to join the today table with the historical table, so they act as one. Presently the query for week view is:
SELECT difference FROM productionlog
WHERE systemID = '$id'
AND DATE(updated) BETWEEN '$weekStart' AND '$weekEnd'
ORDER BY updated
How do I join the today table with the historical table and achieve the above result?
Upvotes: 1
Views: 2642
Reputation: 24406
As mentioned above, you've done a union and your seperate queries are returning their results in their own order. You can put the whole query into brackets, and then order the overall results:
SELECT * FROM (
(SELECT difference, updated FROM productionlog WHERE systemID = '$id' AND DATE(updated) BETWEEN '$weekStart' AND '$weekEnd')
UNION ALL
(SELECT difference, updated FROM productionlog_today WHERE systemID = '$id' AND DATE(updated) BETWEEN '$weekStart' AND '$weekEnd' ORDER BY updated)
) as result
ORDER BY result.updated
Upvotes: 1
Reputation: 6802
Try this query,
SELECT difference FROM productionlog WHERE systemID = '$id' AND DATE(updated) BETWEEN '$weekStart' AND '$weekEnd'
UNION ALL
SELECT difference FROM productionlog_today WHERE systemID = '$id' AND DATE(updated) BETWEEN '$weekStart' AND '$weekEnd' ORDER BY updated
Also read UNION
Upvotes: 1