【问题标题】:Modifying the contents of a result returned by a Oracle Sql query修改 Oracle Sql 查询返回的结果的内容
【发布时间】:2015-01-26 09:59:17
【问题描述】:

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

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

select * from customer;

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

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

例如 在状态列中: 0 表示 Prospect,1 表示 Active,2 表示 Existing。

问题:

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

select * from customer;

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

【问题讨论】:

  • 能否贴一些代码,如何查询数据库,如何读取结果?

标签: java sql database oracle hibernate


【解决方案1】:

我认为您可以通过以下方式使用解码功能:

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

问候 乔瓦

【讨论】:

    【解决方案2】:

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

    如果您不这样做,并且您知道状态编号/描述不会改变,那么您可以使用 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;
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-08
        • 1970-01-01
        • 1970-01-01
        • 2012-01-08
        相关资源
        最近更新 更多