如果未找到任何值,以下查询将返回NULL。如何在这两个内容中分别添加If
,以便Null
然后Not Found
或其他方式返回查询结果?
SET @id = 26;
SELECT
(SELECT id FROM devTable WHERE id <= @id AND country = 'US' ORDER BY id DESC LIMIT 9,1) AS `prev`,
(SELECT id FROM devTable WHERE id >= @id AND country = 'US' ORDER BY id ASC LIMIT 1,1) AS `next`
答案 0 :(得分:2)
使用IFNULL()
:
SET @id = 26;
SELECT
IFNULL(
(SELECT id FROM devTable WHERE id <= @id AND country = 'US' ORDER BY id DESC LIMIT 9,1),
'Not Found') AS prev,
IFNULL(
(SELECT id FROM devTable WHERE id >= @id AND country = 'US' ORDER BY id ASC LIMIT 1,1),
'Not Found') AS next
答案 1 :(得分:-1)
查找T-SQL Exists命令。
像:
IF EXISTS( SELECT STUFF FROM JUNK WHERE ITEMS=5) THEN
BEGIN
SELECT "HAVE JUNK" -- OR PRINT, SET, ...
END
ELSE
BEGIN
SELECT "SORRY, DON'T HAVE JUNK" -- OR PRINT, SET, ...
END
END