我有以下表格:
comments:
id / comments / date
1 comment1 1389986953
2 comment2 1389986935
topics:
id / topics / date
1 topic1 1389986930
2 topic2 1389986940
如何从两个表中选择所有行并按日期排序? 所以它看起来像:
topic1
comment2
topic2
comment1
答案 0 :(得分:0)
尝试SELECT UNION
参考: http://dev.mysql.com/doc/refman/5.0/en/union.html
(SELECT comments AS col, date FROM comments)
UNION
(SELECT topics AS col, date FROM topics)
ORDER BY date;
答案 1 :(得分:0)
您需要使用UNION ALL
来合并两个表的结果,然后将其换行进行排序。
SELECT a.type
FROM (SELECT comments AS type,
date
FROM comments
UNION ALL
SELECT topics,
date
FROM topics) a
ORDER BY a.date;
答案 2 :(得分:0)
SELECT * FROM
(SELECT comments AS mergedField, date
FROM comments
UNION ALL
SELECT topics, date
FROM topics) mergeTable
ORDER BY date ASC