基于匹配另一个字段的 varchar 字段的一部分加入
Join based on part of a varchar field matching another field
我正在寻找连接以在两个 table 上进行左连接。一个 table 上的字符串描述包含另一个 table 的 UID。我只想在描述与 UID 匹配时才进行左连接。我需要 return 交易 table 中的所有记录。
是否可以使用 IF 或条件语句加入 tables 否则将字段保留为空?
这是一个fiddle:
http://sqlfiddle.com/#!3/aaa38/5
交易table:
posted_date person_id description
2015-11-01 2 BZ#1414 Description 1414
2015-11-01 2 Another type of transaction with spaces in it
2015-11-01 3 BZ#1313 Another description 1313
2015-11-01 3 Another_description_without_spaces
Transaction_details table:
id person_id description
1414 2 additional stuff for 1414
1313 3 additional stuff for 1313
结果应如下所示:
posted_date person_id description id person_id description
2015-11-01 2 BZ#1414 Des... 1414 2 additional...
2015-11-01 2 Another typ... NULL NULL NULL
2015-11-01 3 BZ#1313 Ano... 1313 3 additional...
2015-11-01 3 Another_des... NULL NULL NULL
这样加入确实意味着您的交易 table 结构不好(应该有一个单独的列用于您可以用来加入 Transaction_details 的字段)。
话虽如此,你还是可以达到你想要的。您可以在描述以BZ#
开头时加入,然后加入后面的内容,取id的长度来提取数字。这比试图在描述中找到 space 要好,因为可能没有任何 space。
左连接将完成剩下的工作,如果没有匹配项,则用空值填充 tran_details 中的字段。
select *
from
transactions t
left join tran_details d
on t.person_id = d.person_id
and left(t.description, 3) = 'BZ#'
and substring(t.description, 4, len(cast(d.id as nvarchar(50)))) = d.id
如果 BZ#
后跟来自详细信息 table 的 ID,我假设你想加入。
只要前 3 个字符是 Transaction.Description 列
中的 'BZ#',下面的查询就会起作用
select * from Transactions t left outer join Transaction_details td on t.person_id = td.person_id and left(t.description, 3) = 'BZ#' 和
REPLACE(SUBSTRING(t.description, 0, CHARINDEX(' ',t.description, 0)), 'BZ#', '') = td.id
我正在寻找连接以在两个 table 上进行左连接。一个 table 上的字符串描述包含另一个 table 的 UID。我只想在描述与 UID 匹配时才进行左连接。我需要 return 交易 table 中的所有记录。 是否可以使用 IF 或条件语句加入 tables 否则将字段保留为空?
这是一个fiddle: http://sqlfiddle.com/#!3/aaa38/5
交易table:
posted_date person_id description
2015-11-01 2 BZ#1414 Description 1414
2015-11-01 2 Another type of transaction with spaces in it
2015-11-01 3 BZ#1313 Another description 1313
2015-11-01 3 Another_description_without_spaces
Transaction_details table:
id person_id description
1414 2 additional stuff for 1414
1313 3 additional stuff for 1313
结果应如下所示:
posted_date person_id description id person_id description
2015-11-01 2 BZ#1414 Des... 1414 2 additional...
2015-11-01 2 Another typ... NULL NULL NULL
2015-11-01 3 BZ#1313 Ano... 1313 3 additional...
2015-11-01 3 Another_des... NULL NULL NULL
这样加入确实意味着您的交易 table 结构不好(应该有一个单独的列用于您可以用来加入 Transaction_details 的字段)。
话虽如此,你还是可以达到你想要的。您可以在描述以BZ#
开头时加入,然后加入后面的内容,取id的长度来提取数字。这比试图在描述中找到 space 要好,因为可能没有任何 space。
左连接将完成剩下的工作,如果没有匹配项,则用空值填充 tran_details 中的字段。
select *
from
transactions t
left join tran_details d
on t.person_id = d.person_id
and left(t.description, 3) = 'BZ#'
and substring(t.description, 4, len(cast(d.id as nvarchar(50)))) = d.id
如果 BZ#
后跟来自详细信息 table 的 ID,我假设你想加入。
只要前 3 个字符是 Transaction.Description 列
中的 'BZ#',下面的查询就会起作用select * from Transactions t left outer join Transaction_details td on t.person_id = td.person_id and left(t.description, 3) = 'BZ#' 和 REPLACE(SUBSTRING(t.description, 0, CHARINDEX(' ',t.description, 0)), 'BZ#', '') = td.id