[英]how to set the value of the column in a select statement in oracle?
how to change the output value of the column and it's column name if a certain condition was met? 如果满足某个条件,如何更改列的输出值及其列名? let's say i have table A, it has 3 columns id,salutation, name, surname,gender
假设我有表A,它有3列id,称呼,姓名,性别
i can select all of those by 我可以选择所有这些
select * from A
so what if I want to output the value of salutation based from what gender the data row is, how to do that in select statement, the pseudo code is like this 那么如果我想根据数据行的性别输出称呼值,如何在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
how to do that in pure select statement in oracle? 如何在oracle的纯select语句中做到这一点?
A CASE
statement seems to be what you need. CASE
声明似乎就是您所需要的。 You can do something like this: 你可以这样做:
select yourtable.*,
case when gender = 'Male' then 'Mr. ' || name else 'Ms. ' || name end as namePrefix
from yourtable
The CASE
statement will help you generate the computed column namePrefix
for each row in your table such that either Mr.
or Ms.
is prefixed based on the value for gender
column in that row. CASE
语句将帮助您为表中的每一行生成计算列namePrefix
以便根据该行中的gender
列的值为Mr.
或Ms.
添加前缀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.