如何在select中重命名表中的单个列?

时间:2009-03-05 10:24:42

标签: sql

我有两个表有一个相同的列名,但数据不同。我想加入表,但访问两列(row [“price”],row [“other_price”]):如何在select语句中重命名/别名? (我不想在DB中重命名它们)

8 个答案:

答案 0 :(得分:37)

选择table1.price,table2.price作为other_price .....

答案 1 :(得分:15)

select t1.Column as Price, t2.Column as Other_Price
from table1 as t1 INNER JOIN table2 as t2 
ON t1.Key = t2.Key 
像这样?

答案 2 :(得分:5)

我们AS关键字

select a.Price as PriceOne, b.price as PriceTwo 
from tablea a, tableb b

答案 3 :(得分:1)

您也可以省略AS关键字。
SELECT row1 Price, row2 'Other Price' FROM exampleDB.table1;
在这个选项中,可读性有点降低,但你有理想的结果。

答案 4 :(得分:1)

无需使用SELECT table1.price Table1 Price, table2.price Table2 Price, ..... ,只需使用:

//updated this bean of yours. the others are new
@Bean
public Job importUserJob(JobCompletionNotificationListener listener) throws Exception {
    return jobBuilderFactory.get("importUserJob")
            .incrementer(new RunIdIncrementer())
            .listener(listener)
            .flow(partitionStep())
            .end()
            .build();
}

@Bean
public Step partitionStep(){
    return stepBuilderFactory.get("partitionStep")
            .partitioner(step1()) //leverage the step you already have
            .partitioner("step1", partitioner())
            .gridSize(10) //# of threads
            .taskExecutor(taskExecutor())
            .build();
}

@Bean
public Partitioner partitioner() {
    //Use this partitioner to add ranges for your reader
    //NOTE: your reader needs to be in @StepScope to pull from the Step Execution Context
    return new YourCustomPartitioner();
}

@Bean
public TaskExecutor taskExecutor() {
    return new SimpleAsyncTaskExecutor();
}

答案 5 :(得分:1)

如果像我一样,对某列执行此操作,然后通过COALESCE / array_to_json / ARRAY_AGG / row_to_json(PostgreSQL),并希望将大写字母保留在列名中,请用双引号将该列名括起来,如下所示:< / p>

SELECT a.price AS "myFirstPrice", b.price AS "mySecondPrice"

没有引号(以及使用这些功能时),我在camelCase中的列名将丢失大写字母。

答案 6 :(得分:0)

如果您使用的是sql server,请在代码中的查询中使用括号或单引号括起来。

答案 7 :(得分:0)

您可以选择另一个选项:

select price = table1.price , other_price = table2.price from .....

参考:

如果您对aliasing a column using “=” versus “as”的效果感到好奇。