将列名称赋给值行

时间:2016-02-16 11:00:06

标签: sql union alias derby

在Apache Derby中,我使用以下语句生成一些值:

select * from (
    select 10 as DISTANCE_KM from SYSIBM.SYSDUMMY1 union
    select 15 as DISTANCE_KM from SYSIBM.SYSDUMMY1 union
    select 18 as DISTANCE_KM from SYSIBM.SYSDUMMY1 union
    select 21 as DISTANCE_KM from SYSIBM.SYSDUMMY1 union
    select 25
) as DISTANCE

返回:

|DISTANCE_KM|
|-----------|
|10         |
|15         |
|18         |
|21         |
|25         |

有一种更简洁的方法可以生成类似的结果:

select * from (values 10, 15, 18, 21, 25) as DISTANCE

返回:

|1          |  <---- I'd like this column to be called DISTANCE_KM
|-----------|
|10         |
|15         |
|18         |
|21         |
|25         |

有没有办法将返回的列别名为DISTANCE_KM?这是我尝试过的,但它没有成功:

select "1" as DISTANCE_KM from (values 10, 15, 18, 21, 25) as DISTANCE

P.S。我不允许更改架构 - 因此我无法将值存储在自己的表中。

1 个答案:

答案 0 :(得分:1)

我手边没有db2,但这可能有效:

MySqlCommand Command = Connection.CreateCommand();
Command.CommandText = "SELECT employee_address FROM employees WHERE employee_name = 'John'";

如果没有,那么这将有效:

string employee_name = "John"; //value depends on some sort of selection (not user input)

MySqlCommand Command = Connection.CreateCommand();
Command.CommandText = "SELECT employee_address FROM employees WHERE employee_name = '" + employee_name + "'";