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

ako*_*sch 22 sql

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

MrT*_*lly 43

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

  • 简短回答是的.OTOH如果你没有足够的时间来编写25个AS语句,你可以从SysColumns中提取列名并编写一些SQL来创建SQL.简单的方法可能是5分钟...... (3认同)

Mat*_*att 16

select t1.Column as Price, t2.Column as Other_Price
from table1 as t1 INNER JOIN table2 as t2 
ON t1.Key = t2.Key 
Run Code Online (Sandbox Code Playgroud)

像这样 ?


Dea*_*unt 5

我们AS关键字

select a.Price as PriceOne, b.price as PriceTwo 
from tablea a, tableb b
Run Code Online (Sandbox Code Playgroud)


Luc*_*edo 5

如果像我一样,您正在对一列执行此操作,然后该列将经过 COALESCE / array_to_json / ARRAY_AGG / row_to_json (PostgreSQL) 并希望在列名称中保留大写字母,请双引号列名称,如下所示:

SELECT a.price AS "myFirstPrice", b.price AS "mySecondPrice"
Run Code Online (Sandbox Code Playgroud)

如果没有引号(以及使用这些函数时),我的驼峰命名法中的列名称将丢失大写字母。