如何将SELECT EXISTS()查询链接在一起?

时间:2022-04-17 23:50:48

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'

如果A中包含'lorem ipsum'的行,那将返回0或1

I would like to chain this behavior together. So I would get multiple rows, each with a 0 or 1 if that value exists.

我想将这种行为联系在一起。所以我会得到多行,如果存在该值,则每行为0或1。

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');

3 个解决方案

#1


2  

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

#2


0  

Don't have an SQL server handy, but try this:

没有方便的SQL服务器,但试试这个:

SELECT CASE WHEN EXISTS (...) THEN 1 ELSE 0 END
UNION ALL
SELECT CASE WHEN EXISTS (...) THEN 1 ELSE 0 END

#3


0  

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;

#1


2  

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

#2


0  

Don't have an SQL server handy, but try this:

没有方便的SQL服务器,但试试这个:

SELECT CASE WHEN EXISTS (...) THEN 1 ELSE 0 END
UNION ALL
SELECT CASE WHEN EXISTS (...) THEN 1 ELSE 0 END

#3


0  

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;