如何在 SQL 中找到一个表中的所有 X,这些 X 具有来自另一个表的相同 Y 值集
我有表 X,X 中的每条记录在 Y 中都有许多记录。
我想要一个简单的 SQL,它只带来 X 中的记录,而 X 中的记录与 Y 中的记录集相同。
XI 中的每组记录都希望与其他组(具有 Y 中的另一组记录)区分开来并具有某些值。
示例:
1 - 5
- 6
2 - 3
3 - 5
- 6
4 - 3
5 - 7
我希望它返回为:
1 - a
3 - a
2 - b
4 - b
5 - c
I have table X, and each record in X has many records in Y.
I want a simple SQL which brings just the records from X which has the same set of records from Y.
Each group of records from X I want it to be distinguished from other group (which has another set of records from Y) with some value.
Example:
1 - 5
- 6
2 - 3
3 - 5
- 6
4 - 3
5 - 7
I want it back as:
1 - a
3 - a
2 - b
4 - b
5 - c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许我错过了这个问题,但 INTERSECT 运算符不会为您完成这项工作吗?
将返回两个表中存在的所有行。 (这也可以通过简单的 JOIN 来完成,但在这种情况下 INTERSECT 更漂亮)
Maybe I'm missing the problem but won't the INTERSECT operator do this job for you?
will return all rows that exist in both tables. (This could also be done with a simple JOIN, but in this case INTERSECT is prettier)
这看起来像是
wm_concat
的工作或stragg
,它将为您提供独特的价值观,而无需将它们音译为某些内容。通过您的评论,您可以保证子查询中的顺序:
唯一的问题是您的值不会“漂亮”,它们将是
table_x
中所有可能值的串联This looks like a job for
wm_concat
orstragg
, which will give you your unique values without you having to transliterate them to something.And with your comment you can guarantee order in a sub-query:
The only problem is your values won't be "pretty" they'll be a concatenation of all the possible values you have in
table_x