0

我有 2 张桌子。

1.Message - Sender_Number,SMS,DateTime.
2.Replies - Receiver_Number,SMS,DateTime.

我想合并所有列并将 2 个 DateTime 列合并为一列并按时间排序。

例如:

Messages Table:
123 | Hello there. | 2012-10-22 3:50
121 | HI I like U..| 2012-10-22 9:10

Replies Table:
123 | how are u... | 2012-10-22 5:50
121 | HI I like U2 | 2012-10-22 9:30

DESIRED OUTPUT (In 1 table):
123 | Hello there. | 2012-10-22 3:50 
123 | how are u... | 2012-10-22 5:50 
121 | HI I like U..| 2012-10-22 9:10 
121 | HI I like U2 | 2012-10-22 9:30 
4

2 回答 2

2
SELECT *
FROM
(
    SELECT col1, col2, col3 FROM messages
    UNION ALL
    SELECT col1, col2, col3 FROM replies
) x
ORDER BY col3

SQLFiddle 演示

于 2012-10-23T14:39:44.647 回答
0

你需要一个UNION

会是这样的

SELECT * FROM (
SELECT Sender_Number,SMS,DateTime FROM Messages
UNION 
SELECT Receiver_Number,SMS,DateTime FROM Replies
)
ORDER BY DateTime

另外,我不会调用列DateTime,因为它是MySQL. 它可能会导致问题

于 2012-10-23T14:41:58.333 回答