I have 3 table:
MariaDB [test]> DESCRIBE t1;
+-------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(150) | NO | | NULL | |
| time | datetime | NO | | NULL | |
+-------+-----------+------+-----+---------+----------------+
MariaDB [test]> DESCRIBE t2;
+-------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(150) | NO | | NULL | |
| time | datetime | NO | | NULL | |
+-------+-----------+------+-----+---------+----------------+
MariaDB [test]> DESCRIBE t3;
+-------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(150) | NO | | NULL | |
| time | datetime | NO | | NULL | |
+-------+-----------+------+-----+---------+----------------+
I want to join 3 tables and sort all rows with time
field.
How I can do this?
You could use:
SELECT id, name, `time` FROM t1
UNION ALL
SELECT id, name, `time` FROM t2
UNION ALL
SELECT id, name, `time` FROM t3
ORDER BY `time`
The pattern where you have multiple tables with the same schema could indicate poor design.
You don't want a join you want a UNION:
SELECT *
FROM
(
SELECT id, name, time FROM t1
UNION ALL
SELECT id, name, time FROM t2
UNION ALL
SELECT id, name, time FROM t3
) as dt
ORDER BY time
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.