我有3张桌子:
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 | |
+-------+-----------+------+-----+---------+----------------+
我想加入3个表并使用time
字段对所有行进行排序。
我怎么能这样做?
答案 0 :(得分:4)
您可以使用:
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`
您有多个具有相同架构的表的模式可能表示设计不佳。
答案 1 :(得分:2)
您不希望加入您想要的联盟:
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