检索与列出的所有值匹配的行
您好,我需要获取与作为数组列出的所有 groupid 匹配的行,
SELECT user_id,group_id
FROM group_privilege_details g
WHERE g.group_id in (102,101)
如果任何一个 groupid 匹配,这将返回我。但是,我需要用户 ID,其中包含列表中提到的所有组 ID。
Hi I need to get the rows which matches all the groupid listed as an array
SELECT user_id,group_id
FROM group_privilege_details g
WHERE g.group_id in (102,101)
This will return me if any one of the groupid matches. But, I need userid which has all the groupid mention in the list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是 Steven 对通用数组的查询的变体:
只要满足这些要求(问题中未提及)即可工作:
group_privilege_details
中是唯一的。通用解决方案,无论这些先决条件如何都有效:
unnest() 每个基本元素生成一行。
DISTINCT
消除可能的欺骗。子选择对表执行相同的操作。此类查询的广泛选项列表:如何在多通关系中过滤 SQL 结果
Here is a variant of Steven's query for generic arrays:
Works as long as these requirements are met (not mentioned in the question):
group_privilege_details
.A generic solution that works regardless of these preconditions:
unnest() produces one row per base-element.
DISTINCT
removes possible dupes. The subselect does the same for the table.Extensive list of options for this kind of queries: How to filter SQL results in a has-many-through relation
请找到我已解决的查询:
Please find my solved query:
假设您不能有重复的 user_id/group_id 组合:
Assuming that you cannot have duplicate user_id/group_id combinations: