使用连接字段的SQL JOIN [英] SQL JOIN using Concatenated Field

查看:77
本文介绍了使用连接字段的SQL JOIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,表1包含一列,该表构成表2中列的部分值,例如:


Table1 .XName = 123456
Table2.ZName = ABC-123456


我需要创建一个匹配的JOIN这些,但是使用MS-SQL 2008时,我很难进行这项工作。这是我的尝试示例:


SELECT * FROM Table1
左外部联接Table2 ON('ABC-'+ Table1。 XName)= Table2.ZName


我使用哪种类型的JOIN都无关紧要,或者在哪个方向上都失败了。我知道我在这里正在做一些事情,所以如果有人可以指出正确的方向,我将不胜感激。

解决方案

此为我工作(200))

插入
到Table1中
值('123456')

插入
到表2中
值('ABC-123456')

选择*
来自@ Table1
左联接
@ Table2
ON ZName ='ABC-'+ XName

---

123456 ABC-123456

请问您可以发布表的定义以及收到的错误消息吗?


I have two tables, Table1 contains a column with what constitutes a partial value of a column in Table2, for example:

Table1.XName = "123456" Table2.ZName = "ABC-123456"

I need to create a JOIN that matches these up, but with MS-SQL 2008 I'm having trouble making this work. Here's a sample of my attempt:

SELECT * FROM Table1 LEFT OUTER JOIN Table2 ON ('ABC-'+Table1.XName)=Table2.ZName

It doesn't matter what type of JOIN I use, or in which direction either, it fails. I know I'm doing something boneheaded here so if anyone can point me in the right direction I would appreciate it.

解决方案

This works for me:

DECLARE @Table1 TABLE (XName VARCHAR(200))
DECLARE @Table2 TABLE (ZName VARCHAR(200))

INSERT
INTO    @Table1
VALUES  ('123456')

INSERT
INTO    @Table2
VALUES  ('ABC-123456')

SELECT  *
FROM    @Table1
LEFT JOIN
        @Table2
ON      ZName = 'ABC-' + XName

---

123456  ABC-123456

Could you please post the definitions of your tables and the error message you get?

这篇关于使用连接字段的SQL JOIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆