mysql - Select from a table where a condition is fulfilled and another is not -
i have 3 tables:
students (student_id, name, group, year, specialization) scolarships (scolarship_id, name, description, duration) applicants (student_id, scolarship_id)
i need select students have applied scolarship id=2
, have not applied scolarship id=4
so far have query:
select students.name, students.group, students.specialization applicants ap inner join students on students.id = ap.student_id inner join scolarships on scolarships.scolarship_id = ap.scolarship_id ap.scolarship_id = 2;
this selects students have applied scolarship id=2.
how can add condition have applied scolarship 2 haven't applied scolarship 4?
you can use not exists
as
select students.name, students.group, students.specialization applicants ap inner join students on students.id = ap.student_id inner join scolarships on scolarships.scolarship_id = ap.scolarship_id ap.scolarship_id = 2 , not exists( select 1 applicants ap1 ap.student_id = ap1.student_id , ap1.scolarship_id = 4 ) ;
Comments
Post a Comment