【问题标题】:DB2: How to print few columns with conversion along with all other columns without having to write all the names of the columns?DB2:如何打印带有转换的少数列以及所有其他列,而不必写出所有列的名称?
【发布时间】:2011-09-23 04:53:03
【问题描述】:

我有一个有 10 列的表,有 id、f_name、l_name ..... low_range、high_range、...

我想写一个查询来打印我所有的表,但只有这个 low_range 和 high_range 需要转换成十六进制。

我知道两件事:

 1. We can write all the column names and replacing the 
    low_range with hex(low_range) and high_range with high_range. 

但这要求我两个写所有的列名,这似乎有点不公平。

2. We can write a query like:
 select t.*, hex(low_range), hex(high_range) from table t

但它会先给出所有列名,然后是十六进制的必填字段,我不想这样做,因为有重复的信息。

有没有其他干净的方法来实现这件事。

PS:我是数据库新手。

【问题讨论】:

    标签: sql database db2


    【解决方案1】:

    简而言之,没有。您列出的两种方式是指定列输出顺序的唯一两种方式,因此您可以:

    1. select t.* 然后将您的自定义字段附加到末尾(或开头)或

    2. 单独列出每个字段,例如select t.field1, t.field2, hex(t.field3), t.field4...

    您描述的两种方法是唯一的方法。

    【讨论】:

    • 你不认为应该有办法做到这一点吗?
    • 我希望能够写出类似select t.* except t.field1, t.field2 from table t 的东西,我在其中选择了所有内容,除了一些我以后可以插入的字段——但你不能。这就是为什么宽表 SQL 经常会变得如此冗长。这有一些与索引相关的原因(至于为什么这不是一个可行的添加)。
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多