我有两个表格,其中一个列名相同但数据不同。我想把这两个表合并起来,但同时访问这两个列(row["price"], row["other_price"]):在select语句中如何为它们命名别名?(我不想在数据库中重命名它们)
我有两个表格,其中一个列名相同但数据不同。我想把这两个表合并起来,但同时访问这两个列(row["price"], row["other_price"]):在select语句中如何为它们命名别名?(我不想在数据库中重命名它们)
SELECT table1.price, table2.price AS other_price ...
select t1.Column as Price, t2.Column as Other_Price
from table1 as t1 INNER JOIN table2 as t2
ON t1.Key = t2.Key
像这样吗?
select a.Price as PriceOne, b.price as PriceTwo
from tablea a, tableb b
如果你和我一样,正在为一个通过 COALESCE / array_to_json / ARRAY_AGG / row_to_json (PostgreSQL) 处理过后的列进行操作,并且想要保留列名中的大写字母,请在列名两侧加上双引号,例如:
SELECT a.price AS "myFirstPrice", b.price AS "mySecondPrice"
SELECT row1 价格, row2 '其他价格' FROM exampleDB.table1;
如果您正在使用SQL Server,在代码中的查询中使用括号或单引号来表示别名。
不需要使用 AS
,只需使用:
SELECT table1.price Table1 Price, table2.price Table2 Price, .....
你可以选择另一个选项:
select price = table1.price , other_price = table2.price from .....
参考:
如果您对使用“=”与“as”别名列的性能或其他方面感到好奇,请参阅别名列的引用。
*
与AS结合使用吗?)? - U. Windl