Mysql查询减号问题 请考虑下表 user_id city_id role_id 101 1 a 101 2 b 101 3 c

Mysql查询减号问题 请考虑下表 user_id city_id role_id 101 1 a 101 2 b 101 3 c,mysql,sql,nested,Mysql,Sql,Nested,我想要的是 Input Output city id role_id 1,2,3 All user_ids present in city_id 1 and 2 and 3 1,2 c All user_ids present in city_id 1 and 2 and not present in role_id c 1 b,c All us

我想要的是

Input Output city id role_id 1,2,3 All user_ids present in city_id 1 and 2 and 3 1,2 c All user_ids present in city_id 1 and 2 and not present in role_id c 1 b,c All user_ids present in city_id 1 and not present in role_id b,c 2 a,c All user_ids present in city_id 2 and not present in role_id a,c 最简单的方法是什么?注意:表中有大量记录,因此性能也很重要

因此,在上面的示例中,只有当我通过city_id 1,2,3时,才会返回101

我试过了

select user_id, city_id, role_id from foo_table where city_id in (1,2) and role_id not in ('c') group by user_id having count(*) = 2; 及

结果不正确

更新: 我需要这样的东西

select * from (select * from foo_table where city_id in (1)) s1 inner join (select * from foo_table where city_id in (2)) s2 on s1.user_id = s2.user_id and s1.user_id not in (select distinct(user_id) from foo_table where role_id in('c'));
我还在测试它

我感觉你可能在寻找比你下面看到的更复杂的东西,但我试图从字面上解释你的简短问题。i、 e.对于不同的标准集,您需要满足这些标准的用户id列表

MySQL 5.6架构设置:

问题1:

:

问题2:

:

问题3:

:

问题4:

:


这就是我需要的:

Select a.* 
From foo_table a , 
   (Select user_id 
    From foo_table 
    Where city_id in(1,2)   
          and role_id not in('c') 
    Group by user_id 
    Having count(*) = 2) b
Where a.city_id in(1,2)
       And a.city_id = b.city_id;

在输出表中,每条记录的城市id将是用户的输入?是的,上述情况下的输出将是3条记录这看起来很像家庭作业…不是实时应用程序场景..可能是一个坏例子..@Jdoe请看您的问题太广泛,因此反对票。您需要显示您尝试的内容,即您尝试的存储过程没有提供您想要的结果,因此我们可以看到您尝试执行的操作。包括您尝试过的内容、样本输出、表格格式而不是文字等。感谢您的回复。。这里的问题是查询1将返回1、2或3中的源ID。如果您不太神秘,那么我们可能会提供更多帮助。你心里有你想要实现的东西,但基本上我几乎不知道那是什么。查询1确实会返回1、2或3中的ID,因为我相信这是您要求的。花点时间准确地解释您要查找的内容并编辑问题,不要为此使用注释。我已将查询1移动所有查询,以使用group by,查询1和查询2使用having子句,以满足更新的要求
CREATE TABLE foo_table
    (`user_id` int, `city_id` int, `role_id` varchar(1))
;

INSERT INTO foo_table
    (`user_id`, `city_id`, `role_id`)
VALUES
    (101, 1, 'a'),
    (101, 2, 'b'),
    (101, 3, 'c'),
    (101, 4, 'd')
;
Input                    Output
city id  role_id
1,2,3                   All user_ids present in city_id 1 and 2 and 3 

select user_id 
from foo_table 
where city_id in (1,2,3) 
group by user_id
having count(distinct city_id) = 3
| user_id |
|---------|
|     101 |
Input                    Output
1,2       c             All user_ids present in city_id 1 and 2 and not 

select user_id 
from foo_table 
where city_id in (1,2) 
and role_id not in ('c') 
group by user_id
having count(distinct city_id) = 2
| user_id |
|---------|
|     101 |
Input       Output
1     b,c   All user_ids present in city_id 1 and not present in role_id b,c

select user_id 
from foo_table 
where city_id in (1) 
and role_id not in ('b','c') 
group by user_id
| user_id |
|---------|
|     101 |
Input       Output
2     a,c   All user_ids present in city_id 2 and not present  in role_id a,c  

select user_id 
from foo_table 
where city_id in (2) 
and role_id not in ('a','c') 
group by user_id
| user_id |
|---------|
|     101 |
Select a.* 
From foo_table a , 
   (Select user_id 
    From foo_table 
    Where city_id in(1,2)   
          and role_id not in('c') 
    Group by user_id 
    Having count(*) = 2) b
Where a.city_id in(1,2)
       And a.city_id = b.city_id;