我有以下代码用于在文档标题中搜索多个关键字,这些代码可以正常工作。我现在想要一个列,显示每个标题行中的关键字数量和以后代方式排序。例如,如果我正在寻找“产假”,我应该得到这样的东西:
名称 - 找到关键字
产假政策 - 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
答案 0 :(得分:0)
您可以在循环中使用CASE语句修改SELECT子句,例如:
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"
答案 1 :(得分: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