MySQL查询与组

迪特·迪斯梅特(Dieter Desmet)

我有下表:

id  club_id club_option_id created
---|-------|--------------|-------
1  | 1     | 2            | 2015-02-11 16:31:23
2  | 1     | 3            | 2015-02-11 16:31:23
3  | 2     | 2            | 2015-03-06 08:16:02

我想选择同时具有两个选项(2和3)的俱乐部(club_id)

以下查询没有任何结果:

SELECT club_id FROM 
club_options_to_clubs 
WHERE club_option_id = 2 AND club_option_id = 3  
GROUP BY club_id

结果如何获得club_id 1?

阿比克·查克拉博蒂(Abhik Chakraborty)

您可以执行以下操作-

select 
t1.club_id from table_name t1
where t1.club_option_id = 2
and exits (
  select 1 from table_name t2
  where t1.club_id = t2.club_id
  and t2.club_option_id = 3
)

另一种方法是

select club_id from table_name
where club_option_id in (2,3)
group by club_id 
having count(*) = 2

使用上述方法时,如果需要检查函数club_option_id中的多次传递in,然后使用having count(*) = n此处的数字n =in函数中的项目数

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章