我有3张桌子:
Students
(student_id, name, group, year, specialization)
Scolarships
(scolarship_id, name, description, duration)
Applicants
(student_id, scolarship_id)
我需要选择所有已申请奖学金的学生id=2
且尚未申请id=4
到目前为止,我有这个问题:
select students.name, students.group, students.specialization
from applicants ap
inner join students on students.id = ap.student_id
inner join scolarships on scolarships.scolarship_id = ap.scolarship_id
where ap.scolarship_id = 2;
这将选择所有已申请奖学金且id = 2的学生。
如何添加申请奖学金2的条件但尚未申请奖学金4?
答案 0 :(得分:1)
试试这个
select students.name, students.group, students.specialization
from applicants ap
inner join students on students.id = ap.student_id
inner join scolarships on scolarships.scolarship_id = ap.scolarship_id
where ap.scolarship_id = 2 and not exists(select * from applicants as t1
where t1.student_id = students.student_id and t1.scolarship_id = 4)
答案 1 :(得分:0)
您可以使用environ
内容作为
not exists