简体   繁体   English

SQL - 如果一个表中的列中的字符串包含来自连接表的列中的字符串

[英]SQL - If string in a column from one table contains string in column from joined table

I have two tables A and B. Table A has columns ID, Name and Value. 我有两个表A和B.表A有列ID,名称和值。 Among other columns in table B, there is a column called IssueID. 在表B的其他列中,有一个名为IssueID的列。 A.Value has values something like 'ForSymbol12345' and B.IssueID has values like '12345'. A.Value的值类似于'ForSymbol12345',而B.IssueID的值类似'12345'。 I am able to join these two tables on some ID columns in respective tables. 我能够在相应表中的某些ID列上连接这两个表。 However, I only want to select those rows where B.IssueID is present in A.Value value. 但是,我只想选择B.IssueID存在于A.Value值中的那些行。 In other words, B.IssueID is a substring of A.Value. 换句话说,B.IssueID是A.Value的子串。

Can it be done in SQL? 可以在SQL中完成吗? I tried using CONTAINS(string, 'value to search for') but apparently second parameter must be string and cannot be column name. 我尝试使用CONTAINS(字符串,'值搜索')但显然第二个参数必须是字符串,不能是列名。 I tried like 我尝试过

CONTAINS(A.Value, B.IssueID)

But it gives an error saying the second parameter is expected to be String, TEXT_LEX or Variable (a simplified example showing this below) 但它给出了一个错误,说第二个参数应该是String,TEXT_LEX或Variable(一个简化的例子显示如下)

在此输入链接描述

Can someone help me figure this out? 有人可以帮我解决这个问题吗?

Use the LIKE operator with a JOIN . LIKE运算符与JOIN一起使用。

SELECT A.*, B.*
FROM A
INNER JOIN B
ON A.Value LIKE CONCAT('%', B.IssueID, '%') 

CONCAT option mentioned below by evil333 could have worked but I am using SSMS 2008 and CONCAT was introduced in SSMS 2012. So, I found a work around on that here 下面提到的evil333的CONCAT选项可能有效,但我使用的是SSMS 2008和CONCAT是在SSMS 2012中引入的。所以,我在这里找到了解决方法

https://stackoverflow.com/a/21702750/3482656 https://stackoverflow.com/a/21702750/3482656

You can do something like 你可以做点什么

A.value like '%' + cast(B.IssueID as varchar) + '%'

I hope this helps. 我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL一个表上的多个列从另一表连接到列 - SQL Multiple columns on one table joined column from other table 填充了与原始 SQL 表连接的表字符串出现的列 - Column populated with table string occurrences joined to original SQL table 从SQL表的列中检索特定的字符串 - Retrieve specific string from column in SQL table 如何根据 SQL 中另一表的匹配字符串列更新一个表的数字列 - How to update numerical column of one table based on matching string column from another table in SQL 如何检查/选择字符串是否包含表列值中的值? - How to check/select if a string contains value from a table column values? 替换包含表中列的任何结果的字符串的一部分 - Replace part of of a string that contains any results from a column in a table MySql-连接三个表并按一个表中的一列分组 - MySql - Three tables joined and grouped by one column from one table 从SQL表中读取整个字符串列作为字符串数组。 - Reading a whole string column from SQL Table as string array. Oracle 检查表 a 中列中的字符串是否包含表 b 中列中的单词 - Oracle check if a string in column in table a contains word from column in table b 如果另一个表的某个列中的任何值包含相同的“字符串”,则需要添加包含“string”的列 - Need to add column which contains “string” if any of the values in a certain column from another table contain that same “string”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM