如何实现AND、OR、NOT的搜索功能?
searchstr=request.form("txtsearch")
sqlsearch="select * from tablename where title like '%"&searchstr&"%'"
1、是否可以使当searchstr字符串中带有AND、OR、NOT是实现与其对应功能的关键字搜索?如何?
2、对数据库进行搜索的思想如何(例如对得到的字符串进行遍历,当找到AND就换成其它字符什么的)?
---------------------------------------------------------------
可以,这其实是个语法分析的过程
简单一点的话,你让他逐步输入条件,自己组织sqlcondition
直接解析他的xx and xx or xxx not xx的话,你要做一个语法分析(如果你学过编译原理应该很简单)
---------------------------------------------------------------
给这一段代码给你参考
1 dim sql
2sql=""
3if d1<>"请选择" and d1<>"" then
4if sql="" then
5sql=sql & "where left(id,2)='"&d1&"'"
6else
7sql=sql &" and left(id,2)='"&d1&"'"
8end if
9end if
10if d2<>"请选择" and d2<>"" then
11if sql="" then
12sql=sql & "where right(left(id,4),2)='"&d2&"'"
13else
14sql=sql &" and right(left(id,4),2)='"&d2&"'"
15end if
16end if
17
18if sql="" then
19sql="select * from uion_name where flag='0' and left(id,11) in ( select unit_id from update_limit where person_id='"&trim(session("id"))&"'and mode_name_id like '%02%') order by input_time desc"
20else
21sql="SELECT * FROM uion_name " & sql & " and flag='0' and left(id,11) in ( select unit_id from update_limit where person_id='"&trim(session("id"))&"'and mode_name_id like '%02%') order by input_time desc"
22end if
---------------------------------------------------------------
大致步骤应该是:
1.对输入字符串做必要的处理:避免 and and等类似的情况发生
2.按你的需要组织字符串(我做的时候,需要加入(),所以有优先级,所以我用到了后缀表达式),也可以生成一个数组
3.读取每个字串,是and的话,生成类似的代码
( title1 like '%"&search&"%'" or titile2 like '%search%'...) and (title like
'%search2%' or title like '%earch2%' .....)
or 的话
( title1 like '%"&search&"%'" or titile2 like '%search%'...) or (title like
'%search2%' or title like '%earch2%' .....)
not 的话,处理一个serach
( title1 like '%"&search&"%'" or titile2 like '%search%'...)
4.最后把生成的表达式放到where从句后就可以了