一个查询中有两种不同的帖子类型

时间:2017-08-10 12:17:14

标签: php mysql

我在数据库中有两个表:

帖子表

id|name|message|time

通知表

id|name|initiator|type|message|time

我想要的是两个输出两个表不是一个接一个地混合。

类似的东西:

Simon: Reebal want to be friends? 5 minutes ago
Reebal: Yeah sure. 4 minutes ago
Simon and Reebal are friends now (notification)
Reebal: How are you? 2 minutes ago
Simon: Fine and you? 1 minute ago

而不是这样:

Simon and Reebal are friends now (notification)
Simon: Reebal want to be friends? 5 minutes ago
Reebal: Yeah sure. 4 minutes ago
Reebal: How are you? 2 minutes ago
Simon: Fine and you? 1 minute ago

有可能这样做吗?我希望你明白我的意思? 请不要完整的代码,只是正确的建议。

2 个答案:

答案 0 :(得分:1)

建议:

您应该在两个查询中使用union来获得结果。

UNION关键字

答案 1 :(得分:0)

你可以使用UNION。

select * from (
    SELECT name, '' as initiator, '' as type, message, time from post
    UNION
    SELECT name, initiator, type, message, time from notification
) as data order by time desc

这应该对你有用