[英]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.