如果嵌套子查询的计数为 0,我希望 SELECT 查询返回特定值...
SELECT
( SELECT (CASE COUNT(*) = 0 THEN 'TRUE' ELSE 'FALSE' END)
FROM List
WHERE Status = 1
AND Deleted = 1
) AS Status
这不起作用这种语法有什么问题?
如果嵌套子查询的计数为 0,我希望 SELECT 查询返回特定值...
SELECT
( SELECT (CASE COUNT(*) = 0 THEN 'TRUE' ELSE 'FALSE' END)
FROM List
WHERE Status = 1
AND Deleted = 1
) AS Status
这不起作用这种语法有什么问题?
我相信你想要一个看起来像这样的案例陈述:
CASE
WHEN EXISTS(SELECT * FROM list WHERE Status = 1 AND Deleted = 1) THEN 'FALSE'
ELSE 'TRUE'
END
您的查询缺少 CASE 语句中的 WHEN 关键字。
SELECT
( SELECT (CASE WHEN COUNT(*) = 0 THEN 'TRUE' ELSE 'FALSE' END)
FROM List
WHERE Status = 1
AND Deleted = 1
) AS Status
您可以使用它,也可以通过删除外部 SELECT 语句来简化查询。而且,括号也是可选的。
SELECT CASE WHEN COUNT(*) = 0 THEN 'TRUE' ELSE 'FALSE' END
FROM List
WHERE Status = 1
AND Deleted = 1