下面的查询在服务表中存在 ID 的记录时返回“找到”,但在服务表中不存在记录时不返回“未找到”。我不知道为什么。
select case when exists (select idaccount from services where idaccount
=s.idaccount )
then 'Found'
else 'NotFound' end as GSO
from services s
where s.idaccount in ( 1421)
最佳答案
你的查询只会返回一个存在的行,所以 case 语句是多余的,你也可以这样写
SELECT 'Found' FROM services s WHERE s.idaccount IN (1421)
虽然这没什么意义,但你可以这样写:
SELECT CASE
WHEN EXISTS (SELECT 1 FROM services WHERE idaccount = 1421)
THEN 'Found'
ELSE 'NotFound'
END
请注意最外层的 SELECT
中缺少 FROM
子句。编写相同内容的更快方法:
SELECT COALESCE((SELECT 'Found' FROM services WHERE idaccount = 1421), 'NotFound')
关于sql - 使用 case 语句检查 SQL 表中是否存在记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55582331/