SQL:将 CASE 与 AS 结合使用

发布于 2025-01-18 04:01:03 字数 637 浏览 0 评论 0原文

我正在处理一个很长的 SQL 请求,我想在里面使用 CASE 但我不知道如何编写它。 我的请求看起来像:

SELECT (
    SELECT id FROM first_table ft 
    INNER JOIN second_table st ON st.id = ft.id
    INNER JOIN third_table tt ON tt.id = ft.id WHERE tt.second = 0 LIMIT 1
) as key

我想添加类似的内容:

CASE key WHEN 'One' THEN 'Two' END

我尝试在末尾添加此内容,但它不起作用:

SELECT (
    SELECT id FROM first_table ft
    INNER JOIN second_table st
        ON st.id = ft.id
    INNER JOIN third_table tt
        ON tt.id = ft.id WHERE tt.second = 0 LIMIT 1
) as key CASE WHEN key = 'One' THEN 'Two' END

我的目标是沿其值编辑键

I'm working on a long SQL request and I want to use CASE inside but I don't know how to write it.
My request looks like :

SELECT (
    SELECT id FROM first_table ft 
    INNER JOIN second_table st ON st.id = ft.id
    INNER JOIN third_table tt ON tt.id = ft.id WHERE tt.second = 0 LIMIT 1
) as key

I want to add something like :

CASE key WHEN 'One' THEN 'Two' END

I tried to add this at the end but it didn't work :

SELECT (
    SELECT id FROM first_table ft
    INNER JOIN second_table st
        ON st.id = ft.id
    INNER JOIN third_table tt
        ON tt.id = ft.id WHERE tt.second = 0 LIMIT 1
) as key CASE WHEN key = 'One' THEN 'Two' END

My goal is to edit key along its value

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

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

发布评论

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

评论(1

我是有多爱你 2025-01-25 04:01:03

您可以做一个子查询:

SELECT
   key,
   CASE WHEN key = 'One' THEN 'Two' END test_column
FROM
   (
      SELECT 
         id key
      FROM
         first_table ft
         INNER JOIN second_table st ON st.id = ft.id
         INNER JOIN third_table tt ON tt.id = ft.id
      WHERE 
         tt.second = 0
      LIMIT 1
   ) data

You can do a subquery:

SELECT
   key,
   CASE WHEN key = 'One' THEN 'Two' END test_column
FROM
   (
      SELECT 
         id key
      FROM
         first_table ft
         INNER JOIN second_table st ON st.id = ft.id
         INNER JOIN third_table tt ON tt.id = ft.id
      WHERE 
         tt.second = 0
      LIMIT 1
   ) data
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文