sqlplus格式输出

问题描述 投票:-2回答:2

如果字段中的数据='Q',打印'查询'的sql代码是什么?

oracle sqlplus
2个回答
1
投票

欢迎来到SO!

出一个例子。

select decode(dummy, 'X', 'Y') from dual;

对于你的情况,类似于:

select decode(mycol, 'Q', 'Query') mycol from mytable;

祝你们好运!


1
投票

首选是使用 CASE 因为可读性;虽然如@Bjarte所言。DECODE 也可以使用(我就是这么做的,特别是对于简单的情况)。另外,表格有 专栏,不 领域.

总之.., CASE:

SQL> with test (field) as
  2    -- sample data; you already have that and don't type it
  3    (select 'A' from dual union all
  4     select 'Q' from dual union all
  5     select 'B' from dual
  6    )
  7  -- query you need
  8  select field,
  9         case when field = 'A' then 'Answer'
 10              when field = 'Q' then 'Query'
 11              else 'Unknown'
 12         end as result
 13  from test;

F RESULT
- -------
A Answer
Q Query
B Unknown

SQL>
© www.soinside.com 2019 - 2024. All rights reserved.