检索与列出的所有值匹配的行

发布于 2024-12-11 08:22:50 字数 214 浏览 0 评论 0原文

您好,我需要获取与作为数组列出的所有 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 技术交流群。

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

发布评论

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

评论(3

一直在等你来 2024-12-18 08:22:51

以下是 Steven 对通用数组的查询的变体:

SELECT user_id
FROM   group_privilege_details
WHERE  group_id = ANY(my_array)
GROUP  BY 1
HAVING count(*) = array_length(my_array, 1)

只要满足这些要求(问题中未提及)即可工作:

  • (user_id, group_id) 在 group_privilege_details 中是唯一的。
  • 数组只有 1 维基
  • 本数组元素是唯一的

通用解决方案,无论这些先决条件如何都有效:

WITH   ids AS (SELECT DISTINCT unnest(my_array) group_id)
SELECT g.user_id
FROM   (SELECT user_id, group_id FROM group_privilege_details GROUP BY 1,2) g
JOIN   ids USING (group_id)
GROUP  BY 1
HAVING count(*) = (SELECT count(*) FROM ids)

unnest() 每个基本元素生成一行。 DISTINCT 消除可能的欺骗。子选择对表执行相同的操作。

此类查询的广泛选项列表:如何在多通关系中过滤 SQL 结果

Here is a variant of Steven's query for generic arrays:

SELECT user_id
FROM   group_privilege_details
WHERE  group_id = ANY(my_array)
GROUP  BY 1
HAVING count(*) = array_length(my_array, 1)

Works as long as these requirements are met (not mentioned in the question):

  • (user_id, group_id) is unique in group_privilege_details.
  • array has only 1 dimension
  • base array-elements are unique

A generic solution that works regardless of these preconditions:

WITH   ids AS (SELECT DISTINCT unnest(my_array) group_id)
SELECT g.user_id
FROM   (SELECT user_id, group_id FROM group_privilege_details GROUP BY 1,2) g
JOIN   ids USING (group_id)
GROUP  BY 1
HAVING count(*) = (SELECT count(*) FROM ids)

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

画▽骨i 2024-12-18 08:22:51

请找到我已解决的查询:

select user_id,login_name from user_info where user_id in (
SELECT user_id  FROM 
group_privilege_details g WHERE g.group_id in 
(select group_id from group_privilege_details g,user_info u where u.user_id=g.user_id
and login_name='123')  
GROUP BY user_id HAVING count(group_id) = (select count(group_id) 
from group_privilege_details g,user_info u where u.user_id=g.user_id
and login_name='123') ) and login_name!='123'

Please find my solved query:

select user_id,login_name from user_info where user_id in (
SELECT user_id  FROM 
group_privilege_details g WHERE g.group_id in 
(select group_id from group_privilege_details g,user_info u where u.user_id=g.user_id
and login_name='123')  
GROUP BY user_id HAVING count(group_id) = (select count(group_id) 
from group_privilege_details g,user_info u where u.user_id=g.user_id
and login_name='123') ) and login_name!='123'
安穩 2024-12-18 08:22:50

假设您不能有重复的 user_id/group_id 组合:

SELECT user_id,count(group_id)
 FROM group_privilege_details g
WHERE g.group_id in (102,101) 
GROUP BY user_id
HAVING count(group_id) = 2

Assuming that you cannot have duplicate user_id/group_id combinations:

SELECT user_id,count(group_id)
 FROM group_privilege_details g
WHERE g.group_id in (102,101) 
GROUP BY user_id
HAVING count(group_id) = 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文