修改 Oracle Sql 查询返回的结果的内容

Modifying the contents of a result returned by a Oracle Sql query

我们的一位客户要求提供有关客户的信息。

我在下面编写了查询,以提取客户列表。

select * from customer;

Id  |   Name   |  Status
----+----------+-----------
1       Azam        0
2       Kapil       1
3       Osama       2

但问题出在 "Status" 列,它是 java 代码中的一个枚举(我们已将 hibernate 用于 ORM)。 因此,它作为数字存储在数据库中。问题是,我必须替换 "Status" 中的数字 在将其发送给客户之前用常量列,因为客户不理解数字。我这样做 通过生成结果集的 excel 文件并修改 Status 列值。

E.x。 在状态列中: 0 表示前景,1 表示活跃,2 表示现有。

问题:

有没有办法修改查询返回的结果集,仅从 Oracle 获取以下内容:

select * from customer;

Id  |   Name   |   Status
----+----------+------------
1       Azam      Prospect
2       Kapil     Active
3       Osama     Existing

如果您有一个包含状态详细信息的 table,那么只需加入该 table 并输出状态描述即可。

如果你不这样做,并且你知道状态 numbers/description 不会改变,那么你可以使用 case 语句:

select id, name, case when status = 0 then 'Prospect'
                      when status = 1 then 'Active'
                      when status = 2 then 'Existing'
                      ...
                      else null -- can be omitted if null is the desired default, or change the null to the output required
                 end status
from customer;

我想你可以通过以下方式使用解码功能:

select id,name,decode(status,0,'Prospect',1,'Active',2,'Existing) from customer;

问候 乔瓦

除了其他答案,如果你想在数据库中存储枚举常量的字符串值,使用这个映射

@Enumerated(value=EnumType.STRING)
private MyEnum myEnum;