gpt4 book ai didi

mysql - SQL - 如何按公共(public)列对多个表进行排序

转载 作者:行者123 更新时间:2023-11-29 04:06:28 27 4
gpt4 key购买 nike

我有 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 字段对所有行进行排序。

我该怎么做?

最佳答案

你可以使用:

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`

具有相同架构的多个表的模式可能表明设计不佳。

关于mysql - SQL - 如何按公共(public)列对多个表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35828454/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com