我有两个表,internal_mail
和external_mail
,它们相对有不同的列。我试图从两个表中选择所有记录,并按日期列排序,在两个表中都称为 date_of_arrival
。
例如这是internal_mail
:
colunm 1 colunm 2 colunm 3 date_of_arrival
some_record some_record some_record 2017/8/12
..... ..... ..... .....
..... ..... ..... .....
..... ..... ..... .....
..... ..... ..... .....
这是external_mail
:
colunm A colunm B colunm C date_of_arrival
another_record another_record another_record 2017/7/12
..... ..... ..... .....
..... ..... ..... .....
..... ..... ..... .....
..... ..... ..... .....
我想在这里实现的是这样的(在降序的情况下):
colunm 1 colunm 2 colunm 3 column A column B column C date_of_arrival
s_r s_r s_r empty empty empty 2017/8/12
empty empty empty a_r a_r a_r 2017/7/12
s_r s_r s_r empty empty empty 2017/8/12
empty empty empty a_r a_r a_r 2017/7/12
最佳答案
这可以通过完全连接
来完成。由于 MySQL 不支持它,union all
是您的最佳选择。假设您必须在两个表中的日期匹配时在一行中显示记录,则必须按 date_of_arrival 进行聚合。如果不是这种情况,请忽略聚合并仅使用内部查询。
select max(column_1),max(column_2),max(column_3),max(columnA),max(columnB),max(columnC),date_of_arrival
from (select column_1,column_2,column_3,
null as columnA,null as columnB,null as columnC,date_of_arrival
from internal_email
union all
select null,null,null,columnA,columnB,columnC,date_of_arrival
from external_email
) t
group by date_of_arrival
关于mysql - 查询两个具有不同列但一个列之间没有关系的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47717085/