这是什么样的 SQL 连接?

发布于 2024-08-06 15:02:11 字数 240 浏览 4 评论 0原文

假设出于某种原因,我有两个单独的表,employee1 和employee2 中的员工,

我只想将它们添加在一起,就好像它们堆叠在彼此之上一样。

比如:

select all from employee1 and employee2 where name = bubba

我知道我在概括,这最终会出现在 postgres 中,所以如果有任何具体细节,我应该注意,谢谢

Say for some reason I have employees in two separate tables, employee1 and employee2

I just want to add them together, as if they are stacked on top of each other.

something like:

select all from employee1 and employee2 where name = bubba

i know im generalizing, this will be in postgres eventually so if there are any specifics there i should watch for thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

-小熊_ 2024-08-13 15:02:11
SELECT field1, field2, field2 FROM tableA WHERE field1='x'
UNION
SELECT field1, field2, field2 FROM tableB WHERE field1='x'

如果您想要每条记录,甚至重复记录,请使用UNION ALL

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

Use UNION ALL if you want every record, even repeats.

要走就滚别墨迹 2024-08-13 15:02:11

你只想建立一个工会

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

You'll just want to do a union

select * from Employee1 where name = 'bubba'
union
select * from Employee2 where name = 'bubba'
我三岁 2024-08-13 15:02:11

您将需要使用 UNION 关键字

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

You will want to use the UNION keyword

select * from employee1 where name = 'bubba'
union
select * from employee2 where name = 'bubba'
不再见 2024-08-13 15:02:11

在大多数数据库中,您请求的称为 UNION 和像这样写:

select all from employee1 where name = bubba

UNION

select all from employee2 where name = bubba

这来自关系代数的“union”运算符,它的原语之一。

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

In most databases what you are requesting is called a UNION and written like this:

select all from employee1 where name = bubba

UNION

select all from employee2 where name = bubba

This comes from Relational Algebra's "union" operator, one of its primitives.

Please note that UNION follows set unions, namely, it will, for any rows which are duplicate between E1 and E2 tables, only select ONE copy of the row. If you wish to select all copies, use "UNION ALL" operator.

℡寂寞咖啡 2024-08-13 15:02:11

我认为您指的是 UNION 操作。

I think you refer to the UNION operation.

萌逼全场 2024-08-13 15:02:11

如果表具有相同的架构,则

SELECT * FROM employee1 UNION SELECT * FROM employee2

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

If the table have the same schema then

SELECT * FROM employee1 UNION SELECT * FROM employee2

Both tables must have the same number of columns and the columns must be of a similar type.

殤城〤 2024-08-13 15:02:11

我猜它是一个联合

Select * from employee 1 where name = 'bubba'

union

select * from employee2 where name = 'bubba'

如果您也想要重复项,请使用 union all。

Its a union I guess

Select * from employee 1 where name = 'bubba'

union

select * from employee2 where name = 'bubba'

Use union all if you want duplicates as well.

煮酒 2024-08-13 15:02:11

您想要的是“全部联合”:

select * from employee1
union all
select * from employee2;

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

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

What you want is a "union all":

select * from employee1
union all
select * from employee2;

Column types and order must match, or you'll need to provide column lists rather than "*" in the select list. A "where" clause can be added to either or both "select" statements.

Without "all", any duplicate rows between the two queries will be collapsed into a single row. If that's what you want instead, just remove "all".

万劫不复 2024-08-13 15:02:11

正如其他人提到的,您需要UNION。但是,如果您确实希望结果堆叠,则应该使用UNION ALLUNION 将删除重复项,UNION ALL 将包含它们。请参阅http://www.postgresql.org/docs/8.2/interactive /queries-union.html

As others have mentioned, you want UNION. However if you truely want the results stacked, you should use UNION ALL. UNION will remove the dupes, UNION ALL with include them. See http://www.postgresql.org/docs/8.2/interactive/queries-union.html

杯别 2024-08-13 15:02:11

就其价值而言,union all 的执行速度更快,因为它不需要排序来消除两个集合中的重复项。

For what it's worth, union all is quicker to execute as it doesn't have to sort to get rid of duplicates in the two sets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文