2009-03-05 60 views
14

我有兩個表有一個相同的列名稱,但數據不同。我想加入表,但訪問兩列(行[「價格」],行[「other_price」]):如何在select語句中重命名/別名中的一個? (我不希望他們在DB重命名)如何在選擇時重命名錶格中的單個列?

回答

24

選擇table1.price,table2.price作爲other_price .....

+1

如何約25相同的列在兩個表?我是否應該爲所有25欄編寫AS聲明? – HPM 2013-05-18 03:45:14

5

我們AS關鍵字

select a.Price as PriceOne, b.price as PriceTwo 
from tablea a, tableb b 
12
select t1.Column as Price, t2.Column as Other_Price 
from table1 as t1 INNER JOIN table2 as t2 
ON t1.Key = t2.Key 

像這個 ?

0

如果您正在使用sql server,請在代碼查詢中使用括號或單引號括住別名。

1

你也可以省略AS關鍵字。
SELECT row1 Price, row2 'Other Price' FROM exampleDB.table1;
在此選項中,可讀性稍差,但您有期望的結果。

1

沒有必要使用AS,只需使用:

SELECT table1.price Table1 Price, table2.price Table2 Price, ..... 
相關問題