如果字段中的数据='Q',打印'查询'的sql代码是什么?
欢迎来到SO!
出一个例子。
select decode(dummy, 'X', 'Y') from dual;
对于你的情况,类似于:
select decode(mycol, 'Q', 'Query') mycol from mytable;
祝你们好运!
首选是使用 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>