How can I chain together SELECT EXISTS() queries?
I have this simple query
SELECT EXISTS(SELECT a FROM A WHERE a = 'lorem ipsum');
That will return a 0 or 1 if there exists a row in A that contains 'lorem ipsum'
I would like to chain this behavior together. So I would get multiple rows, each with a 0 or 1 if that value exists.
How would I go about doing this?
Update:
It's possible to do it like this but then I get a column for eac开发者_运维问答h result.
SELECT EXISTS(SELECT a FROM A WHERE a = 'lorem ipsum'), EXISTS(SELECT a FROM A WHERE a = 'dolor sit');
SELECT EXISTS(SELECT a FROM A WHERE a = 'lorem ipsum')
UNION ALL
SELECT EXISTS(SELECT a FROM A WHERE a = 'dolor sit');
Or
SELECT EXISTS(SELECT a FROM A WHERE A.a = T.a)
FROM (SELECT 'lorem ipsum' AS a
UNION ALL
SELECT 'dolor sit') T
Don't have an SQL server handy, but try this:
SELECT CASE WHEN EXISTS (...) THEN 1 ELSE 0 END
UNION ALL
SELECT CASE WHEN EXISTS (...) THEN 1 ELSE 0 END
I would use something like this.
SELECT
CASE
WHEN a = 'lorem ipsum' THEN 1
WHEN a = 'dolor sit' THEN 1
ELSE 0
END AS something
FROM A;
精彩评论