gpt4 book ai didi

mysql - 如何将两个查询的结果与排序结合起来?

转载 作者:IT老高 更新时间:2023-10-28 23:59:14 24 4
gpt4 key购买 nike

如何连接 2 个查询的结果,按日期排序?

SELECT * FROM table1 WHERE tag='1'
SELECT * FROM table2 WHERE tag='3'

table1,table2 有相同的字段:id|article|author|tag|date

PS:顺便说一下,标签是 workid

最佳答案

您可以使用 UNION ALL从两个表中获取行:

SELECT id, article, author, tag, date FROM table1 WHERE tag = '1'
UNION ALL
SELECT id, article, author, tag, date FROM table2 WHERE tag = '3'
ORDER BY date

您可能还想考虑重组您的数据库,这样您就不用使用两个表,而是只使用一个带有字段的表来区分每一行的类型。那么查询可以简化为:

SELECT id, article, author, tag, date
FROM yourtable
WHERE (tag, type) IN (('1','type1'), ('3','type2'))
ORDER BY date

关于mysql - 如何将两个查询的结果与排序结合起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5331808/

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