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

发布于 2024-10-27 08:54:13 字数 379 浏览 4 评论 0原文

我有一个简单的查询,

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

如果 A 中存在包含“lorem ipsum”的行,它将返回 0 或 1

我想将此行为链接在一起。所以我会得到多行,如果该值存在,每行都有 0 或 1。

我该怎么做呢?

更新:

可以这样做,但随后我会得到每个结果的一列。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

御弟哥哥 2024-11-03 08:54:13
SELECT EXISTS(SELECT a FROM A WHERE a = 'lorem ipsum')
UNION ALL
SELECT EXISTS(SELECT a FROM A WHERE a = 'dolor sit');

或者

SELECT EXISTS(SELECT a FROM A WHERE A.a = T.a)
FROM (SELECT 'lorem ipsum' AS a 
      UNION ALL 
      SELECT 'dolor sit') T
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
深海蓝天 2024-11-03 08:54:13

手头没有 SQL 服务器,但请尝试以下操作:

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

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
疾风者 2024-11-03 08:54:13

我会用这样的东西。

    SELECT
        CASE
            WHEN a = 'lorem ipsum' THEN 1
            WHEN a = 'dolor sit' THEN 1
            ELSE 0
        END AS something
    FROM A;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文