我正在努力解决的问题是当下面的查询推断结果仍为空时:
SELECT teacher.name, teacher.surname, assignment.code
FROM teacher
JOIN assignment
ON assignment.code = teacher.bsnt
WHERE assignment.working_on_assignment = FALSE
我错了什么?
答案 0 :(得分:0)
如果没有教师有任何匹配的作业记录,那么内部联接会产生空结果集。您可以尝试执行左(外)连接,并将WHERE
条件移动到ON
子句。
SELECT
teacher.name,
teacher.surname,
COALESCE(assignment.code, 'NA') AS code
FROM teacher
LEFT JOIN assignment
ON assignment.code = teacher.bsnt AND
assignment.working_on_assignment = FALSE
如果每位教师都有NA
代码,那么您就会知道没有匹配。