SQL集合操作,每个集合中的列数不同
假设我有集合 1:
1 30 60
2 45 90
3 120 240
4 30 60
5 20 40
和集合 2,
30 60
20 40
我想做某种联合,只保留集合 1 中的第 1、4、5 行,因为集合 1 的后 2 列可以在集合 2 中找到。
我的问题是基于集合的操作坚持相同数量的列。
我曾想过连接列内容,但我觉得这很脏。
有没有“正确”的方法来实现这一目标?
我使用的是 SQL Server 2008 R2
最后,我希望得到
1 30 60
4 30 60
5 20 40
显然我需要去睡觉,因为对 2 列的简单联接有效......谢谢!< /强>
Let say I have set 1:
1 30 60
2 45 90
3 120 240
4 30 60
5 20 40
and set 2
30 60
20 40
I would like to do some sort of union where I only keep rows 1,4,5 from set 1 because the latter 2 columns of set 1 can be found in set 2.
My problem is that set based operations insist on the same numnber of columns.
I've thought of concatenating the columns contents, but it feels dirty to me.
Is there a 'proper' way to accomplish this?
I'm on SQL Server 2008 R2
In the end, I would like to end up with
1 30 60
4 30 60
5 20 40
CLEARLY I need to go sleep as a simple join on 2 columns worked.... Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你实际上是在要求
因此,如果输出仅是表 1 中的第 1、4 和 5 行,那么它是基于集合的操作,可以使用 EXISTS 或 INTERSECT 来完成或加入。对于“相同的列数”,您只需使用 AND 设置 2 个条件即可。这是按行计算的
EXISTS 是最可移植和兼容的方式,允许 table1 中的任何列
INTERSECT 要求每个子句中具有相同的列,但并非所有引擎都支持此功能(SQL Server 自 2005 年起开始支持)
使用 INNER JOIN,如果您有重复项table2 中
val1, val2
的值,那么您将获得比预期更多的行。它的内部结构通常使它比 EXISTS 慢一些 RBDMS 支持多列上的 IN:这不是可移植的,SQL Server 不支持它
编辑:一些背景
关系,它是一个半连接(一个,两个)。
SQL Server 将其作为“左半连接”
相交并存在于SQL Server通常给出相同的执行计划。连接类型是“左半连接”,而 INNER JOIN 是完全“等连接”。
You are literally asking for
So if the output is only rows 1, 4 and 5 from table 1 then it is a set based operation and can be done with EXISTS or INTERSECT or JOIN. For the "same number of column", then you simply set 2 conditions with an AND. This is evaluated per row
EXISTS is the most portable and compatible way and allows any column from table1
INTERSECT requires the same columns in each clause and not all engines support this (SQL Server does since 2005+)
With an INNER JOIN, if you have duplicate values for
val1, val2
in table2 then you'll get more rows than expected. The internals of this usually makes it slower then EXISTSSome RBDMS support IN on multiple columns: this isn't portable and SQL Server doesn't support it
Edit: some background
Relationally, it's a semi-join (One, Two).
SQL Server does it as a "left semi join"
INTERSECT and EXISTS in SQL Server usually give the same execution plan. The join type is a "left semi join" whereas INNER JOIN is a full "equi-join".
您可以使用
union
,与union all
相反,它可以消除重复项:编辑:根据您编辑的问题,您可以使用
排除与第二个表匹配的行>not contains
子查询:如果您想从
table2
中排除行,请省略union all
及其下面的所有内容。You could use
union
which, as opposed tounion all
, eliminates duplicates:EDIT: Based on your edited question, you can exclude rows that match the second table using a
not exists
subquery:If you'd like to exclude rows from
table2
, omitunion all
and everything below it.