0

以下查询在未找到值时返回 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`
4

2 回答 2

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
于 2013-09-25T16:30:24.007 回答
-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
于 2013-09-25T16:25:31.937 回答