将2 varchar与部分匹配进行比较

时间:2014-01-06 11:17:23

标签: asp.net sql sql-server vb.net

  Table1
+---------------+
| varchar1      |
+---------------+
| aaa, aba, aab |
| aac, aca, caa |
+---------------+ 


  Table2
+---------------+
| varchar2      |
+---------------+
| bbb, abb, aba |
| bbc, bcb, cbb |
+---------------+ 

如何比较来自2个不同表的varchar以进行部分匹配?

aaa, aba, aab Table1bbb, abb, aba的{​​{1}}匹配,因为它们都包含Table2

1 个答案:

答案 0 :(得分:0)

正如评论中所提到的,你真的应该将其正常化。这绝对是一个比解析这样的字符串更好的解决方案。但是,如果你真的想,你可以这样做:

Dim varchar1 As IEnumerable(Of String) = {"aaa, aba, aab", "aac, aca, caa"}
Dim varchar2 As IEnumerable(Of String) = {"bbb, abb, aba", "bbc, bcb, cbb"}
Dim varchar1Tokens As IEnumerable(Of String) = (From foo In varchar1
                                                Let tokens = foo.Split(","c)
                                                From bar In tokens
                                                Select bar.TrimEnd)
Dim varchar2Tokens As IEnumerable(Of String) = (From foo In varchar2
                                                Let tokens = foo.Split(","c)
                                                From bar In tokens
                                                Select bar.TrimEnd)
Dim matches As IEnumerable(Of String) = (From item In varchar1Tokens
                                         Where varchar2Tokens.Contains(item)
                                         Select item)

For Each match As String In matches
  Console.WriteLine(match)
Next

Console.ReadLine()