2014-01-06 109 views
0
Table1 
+---------------+ 
| varchar1  | 
+---------------+ 
| aaa, aba, aab | 
| aac, aca, caa | 
+---------------+ 


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

如何比較varchar從2個不同的表進行部分匹配?比較2 varchar部分匹配

這樣aaa, aba, aabTable1bbb, abb, abaTable2比賽,因爲這兩個包含aba

+9

不要在數據庫中存儲逗號分隔的字符串。如果這是正常化的查詢將是微不足道的。 –

+0

你可以修改表格嗎? – Mihai

+0

我知道如何編寫查詢如果表格規範化,但如果我規範化它,我將不得不重新考慮我的web表單GUI和數據輸入/檢索/修改。所以我非常不願意規範化它。 –

回答

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()