我在SO中看过很多关于此事的帖子。但我无法得到答案。 我想查询以检查表中是否存在特定行。如果它存在,它应该返回一个字符串 true并在那里停止搜索,如果不返回false。
答案 0 :(得分:43)
select
case when exists (select true from table_name where table_column=?)
then 'true'
else 'false'
end;
但最好只返回布尔值而不是字符串:
select exists (select true from table_name where table_column=?);
答案 1 :(得分:-13)
扰流:
-- EXPLAIN ANALYZE
WITH magic AS (
WITH lousy AS ( SELECT * FROM one WHERE num = -1)
SELECT 'True'::text AS truth
WHERE EXISTS (SELECT * FROM lousy)
UNION ALL
SELECT 'False'::text AS truth
WHERE NOT EXISTS (SELECT * FROM lousy)
)
SELECT *
FROM magic
;