Combine two tables without a join

dvyio :

I'm fetching and storing inside and outside pressure readings every few minutes, but they're stored in different tables. Additionally, the exact times of these readings aren't the same. (For example, the inside readings are fetched every 10 minutes, but the outside readings are fetched every 5 minutes, and for each, the time might change by a few seconds every time.)

Inside pressure table example (note that the time column is named time_utc in this table)

+---------------------+----------+
|      time_utc       | pressure |
+---------------------+----------+
| 2020-02-13 00:28:51 | 1002.00  |
| 2020-02-13 00:38:49 | 1002.50  |
| 2020-02-13 00:48:55 | 1002.60  |
+---------------------+----------+

Outside pressure table example

+---------------------+----------+
|        time         | pressure |
+---------------------+----------+
| 2020-02-13 00:27:32 | 1001.00  |
| 2020-02-13 00:32:04 | 1001.10  |
| 2020-02-13 00:37:34 | 1001.20  |
| 2020-02-13 00:47:59 | 1001.40  |
+---------------------+----------+

I want to combine these tables so the time values are in one column, and the inside pressure and outside pressure values are in separate columns, ordered by the time.

Output required

+---------------------+-----------------+------------------+
|        time         | inside_pressure | outside_pressure |
+---------------------+-----------------+------------------+
| 2020-02-13 00:27:32 |                 | 1001.00          |
| 2020-02-13 00:28:51 | 1002.00         |                  |
| 2020-02-13 00:32:04 |                 | 1001.10          |
| 2020-02-13 00:37:34 |                 | 1001.20          |
| 2020-02-13 00:38:49 | 1002.50         |                  |
| 2020-02-13 00:47:59 |                 | 1001.40          |
| 2020-02-13 00:48:55 | 1002.60         |                  |
+---------------------+-----------------+------------------+

UNION attempts

I've tried a UNION, which isn't exactly what I need as while it does combine the two tables, it loses the distinction between inside pressure and outside pressure.

UNION Query

SELECT `inside_readings`.`time_utc`, `inside_readings`.`pressure`
FROM   `mydatabase`.`inside_readings` AS `inside_readings`
UNION
SELECT `outside_readings`.`time`, `outside_readings`.`pressure`
FROM   `mydatabase`.`outside_readings` AS `outside_readings`

UNION Result

+---------------------+----------+
|      time_utc       | pressure |
+---------------------+----------+
| 2020-02-13 00:27:32 | 1001.00  |
| 2020-02-13 00:28:51 | 1002.00  |
| 2020-02-13 00:32:04 | 1001.10  |
| 2020-02-13 00:37:34 | 1001.20  |
| 2020-02-13 00:38:49 | 1002.50  |
| 2020-02-13 00:47:59 | 1001.40  |
| 2020-02-13 00:48:55 | 1002.60  |
+---------------------+----------+

JOIN attempts

I also looked into joins, but as there isn't any column in each table that matches the other table, I'm not sure how that would work.

Additionally, some join queries I ran gave hundreds of thousands of rows when there are only about 1,000 readings in each of the two tables (so should give an output with 2,000 rows).

Akina :
SELECT time_utc `time`, pressure inside_pressure, '' outside_pressure
FROM   mydatabase.inside_readings
UNION ALL
SELECT `time`, '', pressure
FROM   mydatabase.outside_readings
ORDER BY `time`

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=11561&siteId=1