我有麻烦了。如何连接2个表查询。如果数据表:
表1: CustomerID : 1,2,3,4,5
客户代码: cus1,cus9,cus4,null,null
顾客名称:罗亚,阿尔穆德纳,杰克,简,弗朗西斯科
表2: CustomerID : 1,2,3,4
客户代码: cus1,cus2,cus9,null
顾客名称:罗亚,何塞,阿尔穆德纳,简
问:什么是查询以显示2个表中的所有名称(没有重复名称)。
谢谢你的回答。
发布于 2018-05-04 06:45:32
您不需要JOIN,您需要一个UNION语句
select distinct name from table1
union
select distinct name from table2如果您使用union all,它将创建重复的,但union本身不会。
如果您想要获得额外的安全,也可以将它封装在select distinct name from ()中。
发布于 2018-05-04 11:41:26
如果每个表中没有重复的名称(如示例数据中的名称),我强烈建议:
select t1.customername
from table1 t1
union all
select t2.customername
from table2 t2
where not exists (select 1 from table1 t1 where t1.customername = t2.customername);这应该有更好的性能。
发布于 2018-05-04 06:50:02
使用联合声明:
select distinct Customername from table1
union
select distinct Customername from table2https://stackoverflow.com/questions/50168759
复制相似问题