我有sql查询,其中我正在传递学生ID' IN'条款。 如果没有与给定id匹配的记录,则不返回任何内容。 即使没有匹配的记录,我希望查询返回虚假或不存在的内容
//伪查询(为了简洁起见,我限制查询一个表,实际查询涉及其他表。)
Select student_id from STUDENTS WHERE s.student_id in (?,?,?);
答案 0 :(得分:1)
申请OR is null
Select s.student_id
from STUDENTS s
WHERE s.student_id in (?,?,?)
or s.student_id is null;
如果加入,请使用左连接:
select s.student_id
from students s
left join othertable o
on o.something = s.something
WHERE s.student_id in (?,?,?)
or s.student_id is null;
如果学生在右边:
select s.student_id
from othertable o
left join students s
on o.something = s.something
and s.student_id in (?,?,?) -- we put this into the join clause so we don't need the "or is null"
;
答案 1 :(得分:0)
IF EXISTs(Select student_id from STUDENTS WHERE s.student_id in (?,?,?))
BEGIN
Select student_id from STUDENTS WHERE s.student_id in (?,?,?)
END
ELSE
select 0