2017-08-02 189 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))

请注意,您的加入会表现不佳。