Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        2年前关闭。
                                                                                            
                
        
我有一个像这样的MySQL数据库:

ID |名称
1 | term1
1 | term2
1 |第三学期
2 | term4
2 |任期5
2 | term6


我想选择包含term1和term3的ID。

或者,例如,term4 AND term5 AND term6。

我怎样才能做到这一点?
任何帮助将不胜感激,谢谢。

最佳答案

要获取必须具有名称作为参数提供的ID的标识符,您可以在下面的查询中使用一些聚合来满足条件

对于第一学期和第三学期

select Id
from demo
where Name in ('term1', 'term3')
group by Id
having count(*) = sum(Name in ('term1', 'term3'));


对于term4&term5&term6

select Id
from demo
where Name in ('term4', 'term5','term6')
group by Id
having count(*) = sum(Name in ('term4', 'term5','term6'));


Demo

10-07 14:27