你如何加入2个查询的结果,按日期排序?
SELECT * FROM table1 WHERE tag='1'
SELECT * FROM table2 WHERE tag='3'
Run Code Online (Sandbox Code Playgroud)
table1,table2具有相同的字段:
id|article|author|tag|date
PS:按方式,标记IS workid
Mar*_*ers 33
您可以使用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
Run Code Online (Sandbox Code Playgroud)
您可能还需要考虑重构数据库,以便不使用两个表而只使用带有字段的单个表来区分每行的类型.然后查询可以简化为:
SELECT id, article, author, tag, date
FROM yourtable
WHERE (tag, type) IN (('1','type1'), ('3','type2'))
ORDER BY date
Run Code Online (Sandbox Code Playgroud)
SELECT *
FROM (SELECT *
FROM table1
UNION
SELECT *
FROM table2) t
ORDER BY t.DATE
Run Code Online (Sandbox Code Playgroud)