作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
如何连接 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/
我是一名优秀的程序员,十分优秀!