繁体   English   中英

连接多个不相关的表但列相同

[英]Joining Multiple unrelated tables but the same columns

我正在尝试联接5-6个具有相同列的表,但数据无关。 例如,表1包含客户1,2,3-表2包含客户4,5,6,依此类推,但没有一个包含同一客户。 如何连接这6个表并在一个表中获取所有数据?

希望我能正确理解您的问题。

请检查以下脚本。

SQL小提琴链接: http ://sqlfiddle.com/#!18/d2396/1

create table customer_1
(
cust_id int
);

insert into customer_1 values(1);
insert into customer_1 values(2);
insert into customer_1 values(3);

create table customer_2
(
cust_id int
);

insert into customer_1 values(4);
insert into customer_1 values(5);
insert into customer_1 values(6);

create table customer_3
(
cust_id int
);

insert into customer_1 values(7);
insert into customer_1 values(8);
insert into customer_1 values(9);


select cust_id from customer_1
union all
select cust_id from customer_2
union all
select cust_id from customer_3;

如果要在所有列下查找所有用户的组合表,请使用UNION或UNION ALL。 如果某些表缺少列,则可以在选择列表中输入null:

https://msdn.microsoft.com/zh-CN/library/ms180026(v=sql.90).aspx

如果没有,您可以交叉连接它们或使用某些父子连接逻辑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM