-2

我在 MySQL 数据库中有两个表

消息

email           message_id   content   time_sent
test@test.ee    1            aaa       2012-11-01 01:09:47
test@test.ee    2            ddd       2012-11-01 01:18:47

回复

message_id   content  time_sent
1            bbb      2012-11-01 01:12:47
1            ccc      2012-11-01 01:14:47
2            eee      2012-11-01 01:19:47

我需要通过某些电子邮件将所有消息与 time_sent 排序的回复合并。
这将是完美的:

email          content  time_sent
test@test.ee   aaa      2012-11-01 01:09:47
               bbb      2012-11-01 01:12:47
               ccc      2012-11-01 01:14:47
test@test.ee   ddd      2012-11-01 01:18:47
               eee      2012-11-01 01:19:47

即使是从哪里开始的提示也会有所帮助。谢谢!

4

2 回答 2

1

Are you looking for something like this?

SELECT email, content, time_sent
  FROM messages
 UNION ALL
SELECT m.email, r.content, r.time_sent
  FROM replies r JOIN messages m
    ON r.message_id = m.message_id
 ORDER BY email, time_sent 

Output:

|        EMAIL | CONTENT |                       TIME_SENT |
|--------------|---------|---------------------------------|
| test@test.ee |     aaa | November, 01 2012 01:09:47+0000 |
| test@test.ee |     bbb | November, 01 2012 01:12:47+0000 |
| test@test.ee |     ccc | November, 01 2012 01:14:47+0000 |
| test@test.ee |     ddd | November, 01 2012 01:18:47+0000 |
| test@test.ee |     eee | November, 01 2012 01:19:47+0000 |

Here is SQLFiddle demo

于 2013-10-22T09:55:20.743 回答
0
Select email as email,content,time_sent
FROM 
message
UNION
Select '' as email, content,time_sent
FROM
replies
于 2013-10-22T09:43:25.833 回答