I have seen many posts about this in SO. But I could not get an answer. I want to the query to check if a particular row exists or not in a table. If it exists, it should return me a string true and stop the search there itself and if not return false.
2 Answers
Spoiler:
-- 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
;
11 Comments
wildplasser
It was intended as a spoiler. The OP is definitely homework, and shows no effort.
Tometzky
What you wrote is gibberish even for me. You don't tutor providing an deliberately obfuscated example that is working but The Daily WTF worthy. You could for example point to documentation of
case and exists, as it is useful only for somebody that knows what to look for.wildplasser
It does work, and produces the correct result. Let the OP explain it to his teacher. And he could look up the syntax for case and exists himself, too. Or for the CTE.
wildplasser
Please note that the OP dis not show any effort, whatsoever. This is just a gimmethecodez question, which can be anwered by anybode sufficiently fluent in SQL (there might even be more methods to get to the same goal than just these two answers)
|