SQL Select仅针对特定组列表中的用户

时间:2018-12-28 16:24:22

标签: sql

我需要选择一个特定用户组中的用户组,但这些用户组中的用户。

User
1
2
3

Group
a
b
c

UserToGroup
1, a
1, b
1, c
2, a
2, c
3, b
3, c

用户必须在a和c组中

Result
2, a
2, c

组和用户都是缩放表,因此不能排除查询中不需要的组。

6 个答案:

答案 0 :(得分:1)

使用:

SELECT *
FROM UserToGroup
WHERE `user` IN (
  SELECT `user`
  FROM UserToGroup
  GROUP BY `user`
  HAVING count(distinct `group`)
     = count(distinct CASE WHEN `group` IN ('a','c') THEN `group` END )
)

演示:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=19bfcfa320ca5e2afbf48a1bbb09a1a1


  

我需要不同的用户。我减少了用户列以简化   例子。

要从Users获取数据,请使用以下查询:

SELECT *
FROM Users
WHERE `user` IN (
  SELECT `user`
  FROM UserToGroup
  GROUP BY `user`
  HAVING count(distinct `group`)
     = count(distinct CASE WHEN `group` IN ('a','c') THEN `group` END )
)

演示:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=7a75d024ce0c871bca6ff3062ca1bc0f

答案 1 :(得分:1)

编辑:我修改了查询以包含用户表中的其他数据。

如果您需要从user和/或group获取其他数据,则可以将简单的JOINNOT EXISTS一起使用,以消除选择项以外的记录清单。它应该优化得很好。 HAVING count(*)依赖于usertorgroupusergroup中没有多个链接记录。如果有任何一个问题,可以进一步过滤查询以消除重复项。还需要传递要搜索的组数。您如何将此值列表传递给查询?

SELECT u.uid AS userid, u.otherstuff
FROM usr u
INNER JOIN usertogroup utg ON u.uid = utg.uid
INNER JOIN grp g ON utg.gid = g.gid
  AND g.gid IN ('a','c')
WHERE NOT EXISTS ( 
    SELECT 1
    FROM usertogroup utg2 
    WHERE utg2.gid NOT IN ('a','c')
       AND utg2.uid = u.uid
)
GROUP BY u.uid, u.otherstuff
HAVING count(*) = 2 /* # of items in list to search. */

演示:

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=f51dbd8a6013d9ec94120cf1ec512735 http://sqlfiddle.com/#!9/96e2a7/1/0

答案 2 :(得分:0)

如果要只使用A和C的用户,则可以执行以下操作:

SELECT 
    MIN(USER_ID) AS USER_ID
FROM
    UserToGroup
HAVING
    MIN(USER_GROUP) = 'a' AND MAX(USER_GROUP) = 'c'

如果它们在A,C和D中,则该查询将对您不起作用。

您也可以尝试

SELECT 
    USER_ID
FROM
    UserToGroup
WHERE USER_GROUP = 'a' 
      AND USER_ID IN (SELECT USER_ID FROM UserToGroup WHERE USER_GROUP = 'c')

此查询获取'a'中的所有用户,并且还会针对同一表查找子查询中的'c'中的用户。

答案 3 :(得分:0)

您可以尝试在edx,rsi,rdi子句中进行条件聚合,该子句检查array_shift($inspiry_breadcrumbs_items); // Removes the Home item #include <iostream> #include <cstdlib> #include <cstring> #include <fstream> using namespace std; void wczytanie(int argc, char* argv[]); int main(int argc, char* argv[]) { wczytanie (argc, argv); system ("pause"); return 0; } void wczytanie(int argc, char* argv[]) { string program; program = argv[1]; fstream fs; fs.open(program, ios::binary | ios::in); fs.close(); } 的计数是否恰好为1,其他所有计数为0。

假设HAVING的列名称为'a''c'

usertogroup

答案 4 :(得分:0)

我希望这就是你想要的:

       SELECT
         CASE WHEN GROUPS IN ('a','c')
         THEN 
          USERS||'-'||GROUPS  
          ELSE
          'USER IN SOME OTHER GROUP'
            END CASE 

         FROM TABLE order by Groups ;

答案 5 :(得分:0)

首先获取恰好属于2个组的用户,然后应用这些组分别为ac的条件:

select u.* from UserToGroup as u 
inner join (
  select userid from UserToGroup 
  group by userid
  having count(*) = 2
) as t
on 
  t.userid = u.userid
  and
  u.groupid IN ('a', 'c')
  and 
  not exists (
    select 1 
    from UserToGroup as uu 
    where 
      uu.userid = t.userid 
      and 
      uu.groupid NOT IN ('a', 'c')
  )

请参见demo