vb 多个关键字 空格 搜索
我现在想完成一个搜索功能, 就是在搜索框key中 输入多个关键字用空格隔开,如: vb 关键字 空格 搜索sz1 = Split(key, " ")
For ii = 0 To UBound(sz1)
key = sz1(ii)
If lei1 = 1 Then
sq = " b1 like '%" & key & "%' or b10 like '%" & key & "%'"
ElseIf lei1 = 2 Then
sq = " b2 like '%" & key & "%'"
If Not IsNumeric(key) Then
htm.hui("关键字必须为纯数字")
End If
ElseIf lei1 = 3 Then
sq = " b3 like '%" & key & "%'"
If Not IsNumeric(key) Then
htm.hui("关键字必须为纯数字")
End If
ElseIf lei1 = 4 Then
sq = " b5 like '%" & key & "%'"
ElseIf lei1 = 5 Then
sq = " b6 like '%" & key & "%'"
ElseIf lei1 = 6 Then
sq = " b7 like '%" & key & "%'"
Else
sq = ""
End If
Dim shuzu As String() = sjk.sel不分页数组("select b0, b1, b2, b3, b6, b7, b8, b4, b5 from dbo.cy_chang " & sq & " order by b0 desc", jihe, wzlei.数据库.分割符.多行且多列)
If UBound(shuzu) = 0 Then
wen.Append("<tr><td height='100' colspan='9' align='center'><strong>没 有 <a href='javascript:history.go(-1);'>返回</a></strong></td></tr></table>")
Exit Sub
End If
Dim sz As String(), qq, ali, tao, wz As String
For i = 0 To UBound(shuzu) - 1
sz = Split(shuzu(i), "<?")
qq = sz(3)
ali = sz(4)
tao = sz(5)
wz = sz(7)
wen.Append("<tr><td> " & (i + 1) & " </td><td nowrap='nowrap'><a href='chang_edit.aspx?id=" & sz(0) & "' target='_blank'>" & sz(1) & "</a> <a href='yuanchan.aspx?cjid=" & sz(0) & "'>产品</a></td><td> " & sz(2) & " </td><td> " & qq & " </td><td> " & sz(8) & " </td><td> " & ali & " </td><td> " & tao & " </td><td>" & wz & "</td><td> " & sz(6) & " </td></tr>")
Next
Next
我用上面的方法做,是每个关键字都搜索一次,显示出来就重复了,还请各位高人 详细详细 指点指点…… --------------------编程问答-------------------- 没人,自己顶 --------------------编程问答-------------------- 高手么,来帮忙啊 --------------------编程问答-------------------- 给高分的哦 --------------------编程问答-------------------- 查出来后,先放到字典中 --------------------编程问答-------------------- 能具体点儿么 --------------------编程问答-------------------- 这个我急需用,请大侠帮忙啊 --------------------编程问答-------------------- 亲么,帮帮忙啊 --------------------编程问答-------------------- 多关键字 只不过是 一个 pinjie=and(a like '%关键字%' or b like '%关键字%' )的 拼接
sql=“select * from biao where 1=1” & pinjie 哪里有问题 怎么写不明白了 写不明白 你贴表结构 --------------------编程问答-------------------- 我说的是vb 语言中 关键字 key --------------------编程问答-------------------- If max = 0 Then '如果max等于0说明只输入了一个关键字,那么就不需要循环处理
'sql = sql & " b1 like '%" & sz1(ii) & "%' or b10 like '%" & sz1(ii) & "%'"
If lei1 = 1 Then
sql = sql & " b1 like '%" & sz1(ii) & "%' or b10 like '%" & sz1(ii) & "%'"
ElseIf lei1 = 2 Then
sql = sql & " b2 like '%" & sz1(ii) & "%'"
ElseIf lei1 = 3 Then
sql = sql & " b3 like '%" & sz1(ii) & "%'"
ElseIf lei1 = 4 Then
sql = sql & " b5 like '%" & sz1(ii) & "%'"
ElseIf lei1 = 5 Then
sql = sql & " b6 like '%" & sz1(ii) & "%'"
ElseIf lei1 = 6 Then
sql = sql & " b7 like '%" & sz1(ii) & "%'"
Else
sql = sql & ""
key = ""
End If
Else '包含多个关键字
For ii = 0 To max '如果关键字很多,我们要求每一个搜索都要匹配每一个关键字,通过循环来实现
If ii = 0 Then
' sql = sql & " (b1 like '%" & sz1(ii) & "%' or b10 like '%" & sz1(ii) & "%') or("
If lei1 = 1 Then
sql = sql & " (b1 like '%" & sz1(ii) & "%' or b10 like '%" & sz1(ii) & "%') or ("
ElseIf lei1 = 2 Then
sql = sql & " b2 like '%" & sz1(ii) & "%' or ("
ElseIf lei1 = 3 Then
sql = sql & " b3 like '%" & sz1(ii) & "%' or ("
ElseIf lei1 = 4 Then
sql = sql & " b5 like '%" & sz1(ii) & "%' or ("
ElseIf lei1 = 5 Then
sql = sql & " b6 like '%" & sz1(ii) & "%' or ("
ElseIf lei1 = 6 Then
sql = sql & " b7 like '%" & sz1(ii) & "%' or ("
Else
sql = ""
key = ""
End If
Else
If ii = max Then '如果循环到最后一个关键字,写入下面sql语句作为结尾
'sql = sql & "b1 like '%" & sz1(ii) & "%' or b10 like '%" & sz1(ii) & "%')"
If lei1 = 1 Then
sql = sql & " b1 like '%" & sz1(ii) & "%' or b10 like '%" & sz1(ii) & "%')"
ElseIf lei1 = 2 Then
sql = sql & " b2 like '%" & sz1(ii) & "%')"
ElseIf lei1 = 3 Then
sql = sql & " b3 like '%" & sz1(ii) & "%')"
ElseIf lei1 = 4 Then
sql = sql & " b5 like '%" & sz1(ii) & "%')"
ElseIf lei1 = 5 Then
sql = sql & " b6 like '%" & sz1(ii) & "%')"
ElseIf lei1 = 6 Then
sql = sql & " b7 like '%" & sz1(ii) & "%')"
Else
sql = ""
key = ""
End If
Else '如果关键自己不是开头的也不是结尾的,那么循坏写入下面的sql语句
'sql = sql & "(b1 like '%" & sz1(ii) & "%' or b10 like '%" & sz1(ii) & "%') or "
If lei1 = 1 Then
sql = sql & " (b1 like '%" & sz1(ii) & "%' or b10 like '%" & sz1(ii) & "%') or "
ElseIf lei1 = 2 Then
sql = sql & " b2 like '%" & sz1(ii) & "%' or "
ElseIf lei1 = 3 Then
sql = sql & " b3 like '%" & sz1(ii) & "%' or "
ElseIf lei1 = 4 Then
sql = sql & " b5 like '%" & sz1(ii) & "%' or "
ElseIf lei1 = 5 Then
sql = sql & " b6 like '%" & sz1(ii) & "%'or "
ElseIf lei1 = 6 Then
sql = sql & " b7 like '%" & sz1(ii) & "%' or "
Else
sql = ""
key = ""
End If
End If
End If
Next
End If
If key <> "" Then
sq = " where "
Else
sq = ""
End If
If key = "" And lei1 <> 0 Then
htm.hui("", "biao.aspx")
End If
sql = "select b0, b1, b2, b3, b6, b7, b8, b4, b5 from dbo.cy_chang " & sq & sql
Dim shuzu As String() = sjk.sel不分页数组(sql, jihe, wzlei.数据库.分割符.多行且多列) --------------------编程问答-------------------- 简单的就是这个
Dim ii, max As Integer, sz1 As String(), sql, sq As String
sq = ""
sql = ""
sz1 = Split(key, " ")
max = UBound(sz1)
If max = 0 Then '如果max等于0说明只输入了一个关键字,那么就不需要循环处理
sql = sql & " b1 like '%" & sz1(ii) & "%' or b10 like '%" & sz1(ii) & "%'"
Else '包含多个关键字
'如果关键字很多,我们要求每一个搜索都要匹配每一个关键字,通过循环来实现
For ii = 0 To max
If ii = 0 Then
sql = sql & " (b1 like '%" & sz1(ii) & "%' or b10 like '%" & sz1(ii) & "%') or("
Else
If ii = max Then '如果循环到最后一个关键字,写入下面sql语句作为结尾
sql = sql & "b1 like '%" & sz1(ii) & "%' or b10 like '%" & sz1(ii) & "%')"
Else '如果关键自己不是开头的也不是结尾的,那么循坏写入下面的sql语句
sql = sql & "(b1 like '%" & sz1(ii) & "%' or b10 like '%" & sz1(ii) & "%') or "
End If
End If
Next
End If
If key <> "" Then
sq = " where "
Else
sq = ""
End If
If key = "" And lei1 <> 0 Then
htm.hui("", "chang.aspx")
End If
sql = "select b0, b1, b2, b3, b6, b7, b8, b4, b5 from dbo.cy_chang " & sq & sql
补充:VB , 网络编程