(php)sql中的查询搜索

efanlx 2008-03-16 09:55:45
象baidu,google一样,输入关键字,多个关键字用空格分开,如何把数据中合适的记录找出来?
如:输入中 国,在数据库中有:中华人民共和国,怎么可以查出来?
请高手指点。
...全文
116 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
magicone 2008-03-17
  • 打赏
  • 举报
回复
正由表达式,把文符串前后和空格都换成%

字段名 like "%中%国%"
snlixing 2008-03-17
  • 打赏
  • 举报
回复
1楼的应该可以
zzxap 2008-03-17
  • 打赏
  • 举报
回复
查询时可以输入多字符串,中间用空格隔开实现模糊查询,查询结果会将关键字用红色突出显示.效果与本站搜索效果一样.

function seachKey(otypestr,keystr) '构造模糊查询语句,otypestr:查询字段,keystr:查询关键字
dim tmpstr,MyArray,I
MyArray = Split(keystr) '默认以空格分组
For I = Lbound(MyArray) to Ubound(MyArray)
if I=0 then
tmpstr=otypestr & " like '%"&MyArray(I)&"%'"
else
tmpstr=tmpstr & " and " & otypestr & " like '%"&MyArray(I)&"%'"
end if
Next
seachKey=tmpstr
end function

function seachResult(contentStr,keyStr)'用红粗突出显示查询结果
Dim MyArray
MyArray = Split(keyStr)
For I = Lbound(MyArray) to Ubound(MyArray)
contentStr=replace(contentStr,MyArray(I),"<font color=red><strong>"&MyArray(I)&"</strong></font>")
next
seachResult=contentStr
end function

用法:

dim strWhere=seachKey(otypestr,keystr)
sql="select * from NEWS where "&strWhere&" order by id desc"
输入:当我们输入的keystr为“我们 函数 数组”时构造的sql语句如下面这样
select * from NEWS where content like '%我们%' and content like '%函数%' and content like '%数组%' order by id desc
myvicy 2008-03-16
  • 打赏
  • 举报
回复
使用模糊查询%
faisun 2008-03-16
  • 打赏
  • 举报
回复
把空格替换为 %
如 title like '%中%国%'

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧