在VB.NET中寻找在MYSQL中搜索字符串模式的有效程序
我正在寻找一个高效的程序,使用vb.net查询mysql数据库,该程序将查找用户请求的字符串,即使请求的字符串拼写错误 至于现在,我有这个程序。但我认为这将占用演出的巨大记忆空间,因此我想听听任何建议或建议在VB.NET中寻找在MYSQL中搜索字符串模式的有效程序,mysql,vb.net,algorithm,search,Mysql,Vb.net,Algorithm,Search,我正在寻找一个高效的程序,使用vb.net查询mysql数据库,该程序将查找用户请求的字符串,即使请求的字符串拼写错误 至于现在,我有这个程序。但我认为这将占用演出的巨大记忆空间,因此我想听听任何建议或建议 循环遍历每个字符串索引,并将每个索引字符替换为“%”(mysql通配符) 每个循环查询mysql数据库以获得结果 如果每个循环的结果在上一个循环循环中已经存在,那么将对其进行比较,这样您就不会得到重复的结果 循环结束后,将根据字符串相似性的百分比对结果进行排序 然后将相应地显示字符串相似性的
谢谢。我为你的第一个问题找到了解决方案。但我需要其他四个的更多信息
Private Function GetWhereClause() As String
Dim strSearch As String = TextBox1.Text
Dim strWhere As String = ""
If Not String.IsNullOrEmpty(strSearch) Then
strWhere = "WHERE"
For i As Integer = 1 To strSearch.Length
Dim tmp As String = strSearch
tmp = tmp.Insert(i, "%")
tmp = tmp.Remove(i - 1, 1)
If Not tmp.StartsWith("%") Then tmp = "%" & tmp
If Not tmp.EndsWith("%") Then tmp = tmp & "%"
strWhere &= IIf(i > 1, " OR", "") & " ( FieldToSearch LIKE '" & tmp & "' ) " & vbCrLf
Next
End If
Return strWhere
End Function
TextBox1.Text=“extra”
的结果将是
WHERE ( FieldToSearch LIKE '%xtra%' )
OR ( FieldToSearch LIKE '%e%tra%' )
OR ( FieldToSearch LIKE '%ex%ra%' )
OR ( FieldToSearch LIKE '%ext%a%' )
OR ( FieldToSearch LIKE '%extr%' )
编辑
搜索多个单词只是另一个for/next循环
Private Function GetWhereClause() As String
Dim strSearch() As String = TextBox1.Text.Split(",")
Dim strWhere As String = ""
If Not String.IsNullOrEmpty(strSearch.ToString) Then
strWhere = "WHERE"
For j As Integer = LBound(strSearch) To UBound(strSearch)
Dim strSplit As String = strSearch(j)
If j >= 1 Then
strWhere &= vbCrLf & " OR ( "
Else
strWhere &= " ( "
End If
For i As Integer = 1 To strSplit.Length
Dim tmp As String = strSplit
tmp = tmp.Insert(i, "%")
tmp = tmp.Remove(i - 1, 1)
If Not tmp.StartsWith("%") Then tmp = "%" & tmp
If Not tmp.EndsWith("%") Then tmp = tmp & "%"
strWhere &= IIf(i > 1, " OR", "") & " ( FieldToSearch LIKE '" & tmp & "' ) " & vbCrLf
Next
strWhere &= " ) "
Next
End If
Return strWhere
End Function
我的searchstring是extra,strings
,结果如下所示:
WHERE ( ( FieldToSearch LIKE '%xtra%' )
OR ( FieldToSearch LIKE '%e%tra%' )
OR ( FieldToSearch LIKE '%ex%ra%' )
OR ( FieldToSearch LIKE '%ext%a%' )
OR ( FieldToSearch LIKE '%extr%' )
)
OR ( ( FieldToSearch LIKE '%trings%' )
OR ( FieldToSearch LIKE '%s%rings%' )
OR ( FieldToSearch LIKE '%st%ings%' )
OR ( FieldToSearch LIKE '%str%ngs%' )
OR ( FieldToSearch LIKE '%stri%gs%' )
OR ( FieldToSearch LIKE '%strin%s%' )
OR ( FieldToSearch LIKE '%string%' )
)
所以,你想通过查询找到所有包含
n-1
字符的单词,比如用户输入,即n
输入的长度?@JoshPart诸如此类,我不确定我是否理解你说的话。用户输入“额外”,你想让代码查询%xtra%
,%e%tra%
,%ex%ra%
,%ext%a%
和%extr%
?@JoshPart是的,就是这样。。对此有什么建议吗?上面列出的只是我脑海中关于如何从输入中获得结果的一个过程。谢谢你。我试试这个。如果输入是多个单词呢?例如:“额外字符串”。我想拆分每个单词,并使用它,如<代码>何处(FieldToeSturle喜欢‘%xTRA%’和FieldToeSturle,如'%Trime%’)或(……继续…)/代码>(等等),使用您的算法。我已经更新了示例代码来考虑您的问题。玩得高兴