具体问题是从类表中选择rollno,它出现在dbms中而不存在于数学中。
rollno sts sub
1 1 dbms
1 0 maths
2 1 dbms
2 0 maths
3 0 dbms
3 1 maths
4 0 dbms
4 0 maths
5 1 dbms
5 1 maths
答案 0 :(得分:0)
您可以使用条件聚合:
select rollno
from mytable
group by rollno
having sum(sub = 'dbms' and sts = 1) > 0
and sum(sub = 'maths' and sts = 1) = 0
这使用了以下事实:true计算结果为1,而在MySQL中,false计算结果为0。
sum(sub = 'dbms' and sts = 1)
查找给定rollno的sub为dbms的行数,该值必须至少为1 sum(sub = 'maths' and sts = 1)
查找sub为给定rollno的数学的行数,该值必须为0. 答案 1 :(得分:0)
你可以使用两个带id的subselect而不是
select roolno
from my_table
where sub in (select roolno from my_table where sub ='dbms')
and sub not in (select roolno from my_table where sub ='maths' )