mysql - 在 VB.NET 中寻找在 MYSQL 中搜索字符串模式的有效过程

标签 mysql vb.net algorithm search

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

就目前而言,我脑子里有这个程序。但我认为这会占用很大的内存空间,影响性能,所以我想得到任何建议或建议。

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

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

谢谢。

最佳答案

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

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%' ) 
 ) 

关于mysql - 在 VB.NET 中寻找在 MYSQL 中搜索字符串模式的有效过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28350834/

相关文章:

vb.net - 将二进制解释为 2 位对

actionscript-3 - 禁词检查算法

php - [PHP-MYSQL]报表查询

mysql - SQL 最大值形式 count()

vb.net - 将 Nullable 属性默认值设置为 Nothing 无法正常工作

java - 计算给定数独谜题的解数?

algorithm - 随机快速排序递归深度

mysql - 如何针对单个 Query 进一步优化此 MySQL 表

php - 查询结果存入一个变量

vb.net - 从标题栏直观地删除/禁用关闭按钮 .NET