我有 2 张 table
ticket_message
msg_id(int)
创建(日期)
response_message
res_id(int)
创建(日期)
如何合并这两个表,以便它们按创建顺序
msg_id已创建
resp_id已创建
最佳答案
我不认为合并意味着加入。
连接意味着您根据某些连接条件将每个表的两行变成一个更大的行。 如果您想从一个表中读取所有行,然后从另一个表中读取所有行,然后对它们进行排序,请使用 UNION 运算符,然后在联合集上使用 ORDERBY。请注意,Union 会使您的索引无法使用,因此排序可能会非常慢!
这样就可以了
(Select ticket_message as message, msg_id as id, created as created, "ticket" as type)
Union
(Select response_message as message, res_id as id, cread as created, "response" as type)
order by created
我添加了一个类型列,以便于区分...
关于mysql - 如何连接2个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5281891/