【问题标题】:SQL QUERY regarding single row as output将单行作为输出的 SQL QUERY
【发布时间】:2022-06-27 23:25:20
【问题描述】:

我想要一个查询,它只根据列中的值返回一个输出行。 我需要的是

表 A 有值

  A
 ----|----
   1 |
   2 |
   3 |

required output
  A
 ----
 yes

所以我需要一个查询,如果表 a 的列中有 3,那么我需要得到的输出是 yes 或 no。 我的代码:-

WITH
number_tb AS (select * from t1),
out_put as (select case when a = 3 then 'yes' else 'no' end a_case from number_tb)
select * from out_put

output:-
a_case
-----
no
no
yes

我只需要单行输出。如果存在 3,那么是或否,我不需要每一行。 有可能吗?

【问题讨论】:

  • 请不要标记垃圾邮件。 PL/SQL 与 Oracle 有关,而 MySQL 和 SQL Server 不是 PostgreSQL。
  • “PostgreSQL 或 SQL”没有意义。 SQL 是一种所有关系型数据库(包括 PostgreSQL)都使用的查询语言

标签: sql postgresql


【解决方案1】:

如果也可以接受适当的布尔真/假值,则可以使用

select exists (select * from t1 where a = 3) as A;

如果您想要一个带有yes/no 的字符串,您可以使用 CASE 表达式将布尔值转换为字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多