您好我想更改Select语句的返回值。我需要在SQL中完成它。 说,我们有
SELECT CourseCode FROM Course WHERE CourseName='?' -- '?' refers to user value
如果用户值为null或者课程名称不在数据库中,我希望select语句返回“Not applicable”。如果可能的话,请给我一个解决方案,我可以在WHERE子句之后编辑代码。感谢
答案 0 :(得分:2)
SELECT IFNULL(Course.CourseCode,'Not applicable') AS CourseCode
FROM (SELECT 1) AS dummy
LEFT JOIN Course ON CourseName='?'
答案 1 :(得分:0)
SELECT CourseCode FROM Course WHERE CourseName='?'
UNION ALL
SELECT 'Not Applicable'
WHERE NOT EXISTS (
SELECT CourseCode FROM Course WHERE CourseName='?'
)
答案 2 :(得分:0)
有一个函数IFNULL(),如果值为null,你可以决定查询返回什么,但它不是在WHERE之后