如果返回的列的值为NULL
,我会尝试显示'否' 的值
我正在使用子查询从数据库中的另一个表中选择列。
我已经尝试IS NULL
,但无法获得我想要的结果。
SELECT r.*,
CASE (SELECT isComplete FROM admission_mt WHERE registration_id = 1)
WHEN NULL THEN 'No' END AS isAdmissionComplete
FROM registration_mt r
WHERE r.schoolyear_yearfrom = 2018;
我得到的isAdmissionComplete列是NULL
而不是'否'
我需要能够显示一个值,这样当我做类似的事情时
resultSet.getString("isAdmissionComplete")
然后我会得到至少一些值,而不是NULL
值,这可能会导致Java中的NULLPointerException
谢谢。
答案 0 :(得分:1)
尝试
WHEN IS NULL THEN
请注意添加的IS
答案 1 :(得分:1)
选择r。*,当isAdmissionComplete为null时的情况为'否'什么时候 isAdmissionComplete不为null然后'是'以...结束 " isAdmissionComplete" FROM registration_mt r WHERE r.schoolyear_yearfrom = 2018;
结果文字
date_registred isAdmissionComplete isAdmissionComplete
2018-01-23(null)否
答案 2 :(得分:1)
有几种方法,您可以检查null
并替换为某个值。
IF(<column_name/expr> IS NULL,'NO','YES')
或强>
CASE
WHEN <column_name/expr> IS NULL
THEN 'NO'
ELSE 'YES'
END
答案 3 :(得分:1)
SELECT r.*,
ifnull(a.isComplete,'No') AS isAdmissionComplete
FROM registration_mt r
left outer join admission_mt a
on a.registration_id = r.registration_id
WHERE r.schoolyear_yearfrom = 2018;
答案 4 :(得分:0)
我意识到registration_id
表中没有匹配的admission_mt
,这就是我无法获取值的原因。感谢大家的意见和建议。我意识到我只需要LEFT JOIN
然后应用CASE WHEN
SELECT r.*,
CASE
WHEN a.isComplete IS NULL THEN 'No'
WHEN a.isComplete = 1 THEN 'Yes' END AS isAdmissionComplete
FROM registration_mt r
LEFT JOIN admission_mt a ON r.registration_id = a.registration_id;