我在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
即使是从哪里开始的提示也会有所帮助。谢谢!
答案 0 :(得分:1)
你在找这样的东西吗?
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
输出:
| 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 |
这是 SQLFiddle 演示
答案 1 :(得分:0)
Select email as email,content,time_sent
FROM
message
UNION
Select '' as email, content,time_sent
FROM
replies