如何加入2张桌子 [英] how to join 2 tables
问题描述
我有2张桌子
机票消息
msg_id(int)
创建(日期)
ticket_message
msg_id(int)
created(date)
response_message
res_id(int)
创建(日期)
response_message
res_id(int)
created(date)
我如何合并这两个表,以便按创建顺序对其进行排序
how can i merge the 2 tables so they are order by created
已创建msg_id
resp_id已创建
msg_id created
resp_id created
推荐答案
我不认为合并意味着加入.
I don't think merge means join.
联接意味着您可以根据某种联接条件将每个表的两行分成一个更大的行. 如果要从一个表中读取所有行,然后从另一个表中读取所有行,然后使用UNION运算符和联合集上的ORDERBY对其进行排序.请注意,Union会使您的索引不可用,因此排序可能会很慢!
Join means you make two rows of each table into a bigger row based on some join condition. If you want to read all rows from one table then from another and then order them use the UNION operator then ORDERBY on the unioned set. Be warned, Union makes your indexes unusable, so Ordering can be quite slow!
那就会
(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
我添加了一个type列,以便于区分...
I've added a type column to make it easier to distinguish...
这篇关于如何加入2张桌子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!