这是什么样的SQL连接?

时间:2009-10-05 14:56:15

标签: sql join union

出于某种原因,我说员工分为两个单独的表:employee1和employee2

我只是想把它们加在一起,好像它们叠在一起。

类似的东西:

select all from employee1 and employee2 where name = bubba

我知道即时通讯,这最终将在postgres中如此,如果有任何细节我应该留意感谢

10 个答案:

答案 0 :(得分:11)

SELECT field1, field2, field2 FROM tableA WHERE field1='x'
UNION
SELECT field1, field2, field2 FROM tableB WHERE field1='x'

如果您想要每条记录,请使用UNION ALL,即使是重复记录。

答案 1 :(得分:4)

你只想做一个工会

select * from Employee1 where name = 'bubba'
union
select * from Employee2 where name = 'bubba'

答案 2 :(得分:3)

您需要使用UNION关键字

select * from employee1 where name = 'bubba'
union
select * from employee2 where name = 'bubba'

答案 3 :(得分:2)

在大多数数据库中,您请求的内容称为UNION,其编写方式如下:

select all from employee1 where name = bubba

UNION

select all from employee2 where name = bubba

这来自Relational Algebra's "union" operator,它是其原始之一。

请注意,UNION遵循设置联合,即,对于E1和E2表之间重复的任何行,它只会选择该行的一个副本。如果要选择所有副本,请使用“UNION ALL”运算符。

答案 4 :(得分:1)

我想你参考UNION操作。

答案 5 :(得分:1)

如果表格具有相同的架构,那么

SELECT * FROM employee1 UNION SELECT * FROM employee2

两个表必须具有相同的列数,并且列必须具有相似的类型。

答案 6 :(得分:1)

我认为它是一个联盟

从员工1中选择*,其中name ='bubba'

联合

从employee2中选择*,其中name ='bubba'

如果你想要重复,也可以使用union all。

答案 7 :(得分:1)

你想要的是“联合所有”:

select * from employee1
union all
select * from employee2;

列类型和顺序必须匹配,或者您需要在选择列表中提供列列表而不是“*”。可以在“select”语句中添加“where”子句。

如果没有“all”,两个查询之间的任何重复行都将折叠为一行。如果这是你想要的,只需删除“全部”。

答案 8 :(得分:0)

正如其他人所说,你想要UNION。但是,如果您确实希望堆叠结果,则应使用UNION ALLUNION将删除欺骗,UNION ALL并包含它们。见http://www.postgresql.org/docs/8.2/interactive/queries-union.html

答案 9 :(得分:0)

对于它的价值,联合所有的执行速度更快,因为它不必排除两组中的重复项。