在经典的asp / vbscript SQL字符串中连接

时间:2012-04-25 20:33:29

标签: sql join concatenation

我想选择列和连接表,其中连接条件连接所选列以及点。例如,

SELECT TBL1.ID,
TBL1.COUNTRY As A,
TBL1.STATE As B,
TBL1.CODE As C
FROM TBL1, TBL2
WHERE TBL2.ID = A+'.'+B+'.'+C

数据是TBL2.ID看起来像' US.NY.61 ',其中美国TBL1.COUNTRY NY < / strong>是TBL1.STATE等等。

更新:SQl小提琴:http://sqlfiddle.com/#!3/26b6b/4

2 个答案:

答案 0 :(得分:0)

尝试这样的事情怎么样

SELECT TBL1.ID,
TBL1.COUNTRY As A,
TBL1.STATE As B,
TBL1.CODE As C
FROM TBL1 
  INNER JOIN TBL2 ON CAST(TBL1.COUNTRY AS varchar(20)) + '.' + CAST(TBL1.STATE AS varchar(20)) + '.' + CAST(TBL1.CODE AS varchar(20)) = CAST(TBL2.ID AS varchar(20))

添加代码以在合并和比较它们之前将4个字段强制转换为varchar。

答案 1 :(得分:0)

SELECT T.name,T2.nam FROM
T INNER JOIN T2
ON T.id = (T2.one + '.' + CAST(T2.two as varchar(50)) + '.' + CAST( T2.thr as varchar(50) ))