首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >连接多个不相关但列相同的表

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

Stack Overflow用户
提问于 2018-02-27 03:28:14
回答 2查看 134关注 0票数 0

我正在尝试连接5-6个表,这些表有相同的列,但数据不相关。例如,表1包含客户1、2、3-表2包含客户4、5、6,依此类推,但它们都不包含相同的客户。如何连接这6个表并获得一个表中的所有数据?

EN

回答 2

Stack Overflow用户

发布于 2018-02-27 03:50:04

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

请检查下面的脚本。

Sql fiddle链接:http://sqlfiddle.com/#!18/d2396/1

代码语言:javascript
运行
复制
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;
票数 0
EN

Stack Overflow用户

发布于 2018-02-27 03:35:43

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

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

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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48995738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档