SQL子查询COUNT CASE

时间:2010-08-12 05:45:13

标签: sql tsql conditional nested-attributes

如果嵌套子查询的计数为0,我希望SELECT查询返回特定值...

SELECT
  ( SELECT (CASE COUNT(*) = 0 THEN 'TRUE' ELSE 'FALSE' END)
    FROM List
    WHERE Status = 1
      AND Deleted = 1
  ) AS Status

这不起作用这个语法有什么问题?

2 个答案:

答案 0 :(得分:8)

我相信你想要一个如下所示的案例陈述:

CASE
 WHEN EXISTS(SELECT * FROM list WHERE Status = 1 AND Deleted = 1) THEN 'FALSE'
 ELSE 'TRUE'
END

答案 1 :(得分:1)

您的查询缺少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