如何在sql查询中用case指定两列

wrrgggsh  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(307)

如何为数据库中的多个列赋值 SQL queryCASE 声明。当我尝试如下所示,我得到的错误。
select case when 1=1 then 'Y' column1, 'Yes' column2 end from dual; ORA-00905: missing keyword 00905. 00000 - "missing keyword" *Cause: *Action: Error at Line: 24 Column: 31 你能帮忙吗。

bmp9r5qi

bmp9r5qi1#

也许你想要:

select 'Y' as column1, 'Yes' as column1
from dual
where 1=1
union all
select NULL, NULL
from dual
where not (1=1);
zzlelutf

zzlelutf2#

使用聚合和 WHERE 条款:

select max('Y') column1, max('Yes') column2 
from dual
where 1=1

结果:

> COLUMN1 | COLUMN2
> :------ | :------
> Y       | Yes

或:

select max('Y') column1, max('Yes') column2 
from dual
where 1=2

结果:

> COLUMN1 | COLUMN2
> :------ | :------
> null    | null

请看演示。

ilmyapht

ilmyapht3#

重复上述步骤 case :

SQL> select case when 1 = 1 then 'Y'
  2              else 'N'
  3         end column1,
  4         --
  5         case when 1 = 1 then 'Y'
  6              else 'N'
  7         end column2
  8  from dual;

C C
- -
Y Y

SQL>

如果您不想重复,可以使用cte:

SQL> with temp as
  2    (select case when 1 = 1 then 'Y'
  3                 else 'N'
  4            end val
  5     from dual
  6    )
  7  select val as column1,
  8         val as column2
  9  from temp;

C C
- -
Y Y

SQL>

或者,更不用说打字了:

SQL> select val as column1,
  2         val as column2
  3  from (select decode(1, 1, 'Y', 'N') va
  4        from dual);

C C
- -
Y Y

SQL>

相关问题