我们如何将属于两个不同表的不同名称的两列合并为一列

时间:2015-06-27 10:06:43

标签: mysql sql select

将表1视为vendor_details,将表2视为customer_details

表1:vendor_details

vendor_Id     vendor_Name
-------------------------------

 v001         ABC Enterprises
 v002         XYZ  Traders

表2:customer_details

customer_Id      customer_Name
----------------------------------

c001             ItUs Software
c002             RTech Solutions

预期产出:

trader_id      trader_name
-----------------------------
v001           ABC Enterprises
v002           XYZ  Traders
c001           ItUs Software
c002           XYZ  Traders

3 个答案:

答案 0 :(得分:2)

union all集合运算符正是您所需要的:

SELECT vendor_id AS trader_id, vendor_name AS trader_name
FROM   vendor_details
UNION ALL
SELECT customer_Id AS trader_id, customer_Name AS trader_name
FROM   customer_details

注意:第二个查询的列别名是多余的,但我添加了它们以使查询更容易阅读。

答案 1 :(得分:1)

select vendor_id as trader_id, vendor_name as trader_name 
from vendor_details
union all
select customer_id, customer_name
from customer_details

答案 2 :(得分:1)

使用union就像这样

select * from 
(select vendor_id as trader_id
,vendor_name as trader_name
from vendor_details
union all
select customer_id as trader_id
,customer_name as trader_name
from customer_details)a