这是什么样的 SQL 连接?
假设出于某种原因,我有两个单独的表,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果您想要每条记录,甚至重复记录,请使用
UNION ALL
。Use
UNION ALL
if you want every record, even repeats.你只想建立一个工会
You'll just want to do a union
您将需要使用 UNION 关键字
You will want to use the UNION keyword
在大多数数据库中,您请求的称为 UNION 和像这样写:
这来自关系代数的“union”运算符,它的原语之一。
请注意,UNION 遵循集合并集,即,对于 E1 和 E2 表之间重复的任何行,它只会选择该行的一个副本。如果您想选择所有副本,请使用“UNION ALL”运算符。
In most databases what you are requesting is called a UNION and written like this:
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.
我认为您指的是 UNION 操作。
I think you refer to the UNION operation.
如果表具有相同的架构,则
两个表必须具有相同的列数,并且列的类型必须相似。
If the table have the same schema then
Both tables must have the same number of columns and the columns must be of a similar type.
我猜它是一个联合
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.
您想要的是“全部联合”:
列类型和顺序必须匹配,否则您需要在选择列表中提供列列表而不是“*”。 “where”子句可以添加到一个或两个“select”语句中。
如果没有“all”,两个查询之间的任何重复行都将折叠成一行。如果这就是您想要的,只需删除“全部”即可。
What you want is a "union all":
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".
正如其他人提到的,您需要
UNION
。但是,如果您确实希望结果堆叠,则应该使用UNION ALL
。UNION
将删除重复项,UNION ALL 将包含它们。请参阅http://www.postgresql.org/docs/8.2/interactive /queries-union.htmlAs others have mentioned, you want
UNION
. However if you truely want the results stacked, you should useUNION ALL
.UNION
will remove the dupes, UNION ALL with include them. See http://www.postgresql.org/docs/8.2/interactive/queries-union.html就其价值而言,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.