如何在oracle中的select语句中设置列的值?

时间:2014-08-29 07:40:41

标签: sql oracle

如果满足某个条件,如何更改列的输出值及其列名? 让我们说我有表A,它有3列 身份,称呼,姓名,性别

我可以通过

选择所有这些
select * from A

那么如果我想根据数据行的性别输出称呼值呢? 如何在select语句中执行此操作,伪代码就像这样

select everything, if the gender is Male, then set salutation to Mr. else, set salutation to Ms. but then change the salutation into alias namePrefix

如何在oracle的纯select语句中执行此操作?

1 个答案:

答案 0 :(得分:3)

CASE声明似乎就是您所需要的。你可以这样做:

select yourtable.*,
case when gender = 'Male' then 'Mr. ' || name else 'Ms. ' || name end as namePrefix
from yourtable

CASE语句将帮助您为表格中的每一行生成计算列namePrefix,以便根据{的值为Mr.Ms.添加前缀该行中的{1}}列。

相关问题