2017-08-02 67 views
2
Table One 


ID Code Amount 
1 2  100 
2 2  200 


Table Two 

ID Key  Description 
1 12  Bag 
2 22  Cap 

我想加入連接兩列的select兩個表中的一個表。在表格中說我想在t1.id + t1時加入它們。代碼= t2.key。在圖形我想22 = 2212 = 12其中在第一面22 or 21t1.id+t1.code連接上的concat 2列值sql

查詢:

Select * 
from table1 AS t1 INNER JOIN table2 AS t2 ON (t1.id +""+ t1.code)= t2.key 

錯誤:

Msg 1038, Level 15, State 4, Line 1 An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.

回答

5

您應該使用''爲空字符串:

Select * 
from table1 AS t1 
INNER JOIN table2 AS t2 ON (t1.id +''+ t1.code)= t2.[key] 
-- key is reserved keyword so you need to quote it 

或者CONCAT

Select * 
from table1 AS t1 
INNER JOIN table2 AS t2 ON CONCAT(t1.id, t1.code)= t2.[key]; 

Rextester Demo

如果列INT還需要扮演他們喜歡:CAST(t1.id AS VARCHAR(10))

請注意,您的加入會表現不佳。