如何将两个查询的结果与排序结合在一起? [英] How do I combine the results of two queries with ordering?
本文介绍了如何将两个查询的结果与排序结合在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您如何将2个查询的结果按日期排序?
How do you join the results of 2 queries, ordering by date?
SELECT * FROM table1 WHERE tag='1'
SELECT * FROM table2 WHERE tag='3'
table1,table2具有相同的字段:
id|article|author|tag|date
table1,table2 have the same fields:
id|article|author|tag|date
PS:顺便说一句,标签是workid
PS: BY THE WAY, tag IS workid
推荐答案
You can use UNION ALL
to get rows from both tables:
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
您可能还想考虑重组数据库,以使您不使用两个表,而只使用一个带有字段的单个表来区分每一行的类型.然后,查询可以简化为:
You may also want to consider restructuring your database so that instead of using two tables you use just a single table with a field to distinguish the type of each row. Then the query can simplify to:
SELECT id, article, author, tag, date
FROM yourtable
WHERE (tag, type) IN (('1','type1'), ('3','type2'))
ORDER BY date
这篇关于如何将两个查询的结果与排序结合在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文