简体   繁体   中英

MySQL join two tables where date between two dates

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM