Home > Backend Development > PHP Tutorial > PHP+MYSQL 多关键词+模糊搜索+匹配度排序,请教怎么实现?

PHP+MYSQL 多关键词+模糊搜索+匹配度排序,请教怎么实现?

WBOY
Release: 2016-06-23 14:23:27
Original
1618 people have browsed it

mysql php 模糊搜索 排序

已经用MYSQL 的 UNION 实现了一个,但是效率太差,请教论坛上的高人有没有更好的方法。

回复讨论(解决方案)

将你的表结构贴和sql贴出来

本帖最后由 xuzuning 于 2013-02-23 10:18:55 编辑

sql:
(SELECT a.`askid`,a.`catid`,a.`title`,a.`status`,a.`addtime`,a.`reward`,a.`answercount`,b.`message` FROM `phpcms_ask` a LEFT JOIN `phpcms_ask_posts` b ON a.`askid`=b.`askid` WHERE (a.status=3 OR a.status=5) AND (LOCATE('中国北京天安门',a.`title`)>0) AND b.`isask`=1)UNION (SELECT a.`askid`,a.`catid`,a.`title`,a.`status`,a.`addtime`,a.`reward`,a.`answercount`,b.`message` FROM `phpcms_ask` a LEFT JOIN `phpcms_ask_posts` b ON a.`askid`=b.`askid` WHERE (a.status=3 OR a.status=5) AND (LOCATE('中国',a.`title`)>0 AND LOCATE('北京',a.`title`)>0 AND LOCATE('天安门',a.`title`)>0) AND b.`isask`=1) UNION (SELECT a.`askid`,a.`catid`,a.`title`,a.`status`,a.`addtime`,a.`reward`,a.`answercount`,b.`message` FROM `phpcms_ask` a LEFT JOIN `phpcms_ask_posts` b ON a.`askid`=b.`askid` WHERE (a.status=3 OR a.status=5) AND (LOCATE('中国',a.`title`)>0 AND LOCATE('北京',a.`title`)>0) AND b.`isask`=1)UNION(SELECT a.`askid`,a.`catid`,a.`title`,a.`status`,a.`addtime`,a.`reward`,a.`answercount`,b.`message` FROM `phpcms_ask` a LEFT JOIN `phpcms_ask_posts` b ON a.`askid`=b.`askid` WHERE (a.status=3 OR a.status=5) AND (LOCATE('北京',a.`title`)>0 AND LOCATE('天安门',a.`title`)>0) AND b.`isask`=1)UNION(SELECT a.`askid`,a.`catid`,a.`title`,a.`status`,a.`addtime`,a.`reward`,a.`answercount`,b.`message` FROM `phpcms_ask` a LEFT JOIN `phpcms_ask_posts` b ON a.`askid`=b.`askid` WHERE (a.status=3 OR a.status=5) AND (LOCATE('中国',a.`title`)>0 OR LOCATE('北京',a.`title`)>0 OR LOCATE('天安门',a.`title`)>0) AND b.`isask`=1)LIMIT 0,20
Copy after login

phpcms_ask表: askid catid title tags reward userid username addtime endtime status flag answercount anonymity hits ischeck


phpcms_ask_posts表:pid askid isask message addtime reply rptime candidate optimal reversion userid status anonymity username

既然你不需要计算出现的次数,那么合起来不是更好

SELECT a.`askid`,a.`catid`,a.`title`,a.`status`,a.`addtime`,a.`reward`,a.`answercount`,b.`message`  FROM `phpcms_ask` a LEFT JOIN `phpcms_ask_posts` b ON a.`askid`=b.`askid` WHERE (a.status=3 OR a.status=5) AND b.`isask`=1 AND (LOCATE('中国',a.`title`)>0 OR LOCATE('北京',a.`title`)>0 OR LOCATE('天安门',a.`title`)>0)
Copy after login

若要计算匹配度,也只需
select sign(LOCATE('中国',a.`title`))+sign(LOCATE('北京',a.`title`))+sign(LOCATE('天安门',a.`title`)) as 匹配数 from ...

既然你不需要计算出现的次数,那么合起来不是更好
SQL code?123SELECT a.`askid`,a.`catid`,a.`title`,a.`status`,a.`addtime`,a.`reward`,a.`answercount`,b.`message`  FROM `phpcms_ask` a LEFT JOIN `phpcms_ask_posts` b……

感谢版主,您的方法非常有效!!现在速度快了很多。

匹配度排序该如何实现呢 

若要计算匹配度,也只需
select sign(LOCATE('中国',a.`title`))+sign(LOCATE('北京',a.`title`))+sign(LOCATE('天安门',a.`title`)) as 匹配数 from ...  

版主 请问:按指定的??字在字段中出?的次?排序 该如何实现呢? 只在一个字段中查询

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template