Mysql 从多个列合并
我有两张这样的桌子:Mysql 从多个列合并,mysql,Mysql,我有两张这样的桌子: New_Customer Order_Id Invoice Aaron 5 5 John 56 44 -------------------------- Old_Customers Order_Id Ticket_Num Casey 44 11 Jonathan
New_Customer Order_Id Invoice
Aaron 5 5
John 56 44
--------------------------
Old_Customers Order_Id Ticket_Num
Casey 44 11
Jonathan 1 4
我想要什么
All_Customers All_Order_Ids
Aaron 5
John 56
Casey 44
Jonathan 1
我能用一个select语句只获得这两个列的并集(没有其他列)吗
我试过了
select tb1.New_Customer union tb2.Old_Customers as All_Customers, tb1.Order_Id union tb2.Order_Id as All_Order_Ids;
但我在语法问题上遇到了一个错误。有人能告诉我我做错了什么吗?用第一条语句中的列别名执行两条select语句。这些别名将被拾取并在结果集中使用
create table t1
( new_customer varchar(30) not null,
order_id int not null,
invoice int not null
);
insert t1 values ('aaron',5,5),('john',56,44);
create table t2
( old_customers varchar(30) not null,
order_id int not null,
ticket_num int not null
);
insert t2 values ('casey',44,11),('jonathan',1,4);
查询:
我不太明白。我正试着用一种选择的方式来做这件事。我同意,我的第一个版本说“好了,巴德”,值得投反对票
select new_customer as All_Customers,order_id as All_Order_Ids from t1
union
select old_customers,order_id from t2
+---------------+---------------+
| All_Customers | All_Order_Ids |
+---------------+---------------+
| aaron | 5 |
| john | 56 |
| casey | 44 |
| jonathan | 1 |
+---------------+---------------+