Search code examples
mysqlexistschaining

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 each result.

SELECT EXISTS(SELECT a FROM A WHERE a = 'lorem ipsum'), EXISTS(SELECT a FROM A WHERE a = 'dolor sit');

Solution

  • 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