【问题标题】:looking for efficient procedure in VB.NET on searching string pattern in MYSQL在 VB.NET 中寻找在 MYSQL 中搜索字符串模式的有效过程
【发布时间】:2015-02-05 17:57:20
【问题描述】:

我正在寻找一个有效的程序来使用 vb.net 查询 mysql 数据库,即使请求的字符串拼写错误,也会查找用户请求的字符串。

就目前而言,我有这个程序。但我认为这会在性能上占用很大的内存空间,所以我希望有任何建议或建议。

  1. 遍历每个字符串索引并将每个索引字符替换为“%”(mysql 通配符)
  2. 每个循环查询 mysql 数据库以获取结果
  3. 如果每个循环的结果已经存在于上一个循环循环中,则将对其进行比较,这样您就不会得到重复的结果
  4. 循环结束后,将根据字符串相似度百分比对结果进行排名
  5. 字符串相似度的结果会相应显示

例如,用户将输入“extra”和一些数据库记录中包含“extreme”、“extravagance”、“extraordinary”、“trash”、“programming”的字符串,查询应返回“extragance” ","非凡","极端"。

谢谢。

【问题讨论】:

  • 那么,您想要一个查询来查找所有包含n-1 字符的单词,例如用户输入,n 输入的长度?
  • @JoshPart 类似的东西,我不确定我是否理解你所说的。
  • 用户输入“额外”,您希望代码查询%xtra%%e%tra%%ex%ra%%ext%a%%extr%
  • @JoshPart 是的,就是这样.. 对此有什么建议吗?

标签: mysql vb.net algorithm search


【解决方案1】:

我已经为您的第一个问题找到了解决方案。但我需要其他四个的更多信息。

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

我的搜索字符串是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%' ) 
 ) 

【讨论】:

  • 上面列出的只是我脑海中关于如何从输入中获取结果的过程。谢谢你。我试试这个。
  • 如果输入的是多个单词怎么办?例如:“额外的字符串”。我想使用您的算法拆分每个单词并像WHERE ( FieldToSearch LIKE '%xtra%' AND FieldToSearch LIKE '%tring%') OR (...continue...)(等等)一样使用它。
  • 我已更新示例代码以考虑您的问题。玩得开心。
猜你喜欢
  • 2014-02-23
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
  • 2017-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多