0

我有以下代码用于在文档标题中搜索多个关键字,效果很好。我现在想要一列显示在每个标题行中找到的关键字数量并以后代方式排序。例如,如果我正在搜索“产假”,我应该得到如下信息:

名称 - 找到的关键字

产假政策 -- 2

年假政策——1

给准妈妈的孕产建议——1

希望它是有道理的,非常感谢!

    Dim s As String = TextBox1.Text

    Dim SqlQuery As String = "SELECT Doc_LibraryTable.DocID, Doc_LibraryTable.DocName, Doc_LibraryTable.DocType WHERE Doc_LibraryTable.DocType = DocType"

    Dim ints As String() = s.Split(" ")
    Dim i As Integer 

    If UBound(ints) >= 1 Then
        SqlQuery += " AND (Doc_LibraryTable.DocName LIKE '%" + ints(0) + "%'"       

        For i = 1 To UBound(ints)
            If Not ints(i) = "of" And Not ints(i) = "the" And Not ints(i) = "in" And Not ints(i) = "or" Then
                SqlQuery += " OR Doc_LibraryTable.DocName LIKE '%"
                SqlQuery += ints(i) + "%'"
            End If
        Next



    ElseIf UBound(ints) < 1 Then
       For i = 0 To UBound(ints)
            SqlQuery += " AND (Doc_LibraryTable.DocName LIKE '%"
            SqlQuery += ints(i) + "%"
        Next

   End If

     If UBound(ints) >= 1 Then
        SqlQuery += ")"
    ElseIf UBound(ints) < 1 Then
        SqlQuery += "')"
    End If

    SqlDataSource2.SelectCommand = SqlQuery
    GridView1.DataSource = SqlDataSource2
4

2 回答 2

0

您可以在循环中修改您的 SELECT 子句并使用 CASE 语句来执行此操作,例如:

Dim SqlQuery As String = "SELECT Doc_LibraryTable.DocID, Doc_LibraryTable.DocName, Doc_LibraryTable.DocType, "
Dim sep as String = ""
For i = 0 To UBound(ints)
    SqlQuery += sep + "CASE WHEN Doc_LibraryTable.DocName LIKE '%" + ints(i) + "%' THEN 1 ELSE 0 END"
    sep = " + "
Next
SqlQuery += " as MatchCount WHERE Doc_LibraryTable.DocType = DocType"
于 2009-09-10T14:39:02.200 回答
0

另一种有点混乱的方法是做类似的事情

select ...   
 , len(replace([DocName],'Maternity leave','Maternity leave' + '*')) - len([DocName])
  As NameHits

我使用以下代码对此进行了测试:-

set nocount on;
declare @t as table([name] varchar(10))
insert into @t values ('a bb c')
insert into @t values ('aa bc c')

declare @find varchar(5)
set @find = 'bc'

select
    *, 
    len(replace([name],@find,@find + '*')) - len(name)

from @t
于 2009-09-10T14:58:56.890 回答