Home > Backend Development > PHP Tutorial > mysql 搜索结果依照关键字排序

mysql 搜索结果依照关键字排序

WBOY
Release: 2016-06-13 12:06:50
Original
1188 people have browsed it

mysql 搜索结果按照关键字排序
现在需要搜索一个字段中包含:猪,狗,牛三个词的数据,
请问,怎么才能让结果按照指定关键词排序,比如让结果数据按照其中包含了猪的排在前面,包含了狗的排在第二,牛的排在第三。

例如:
猪啊
牛啊
狗啊
狗啊
猪啊
牛啊

要让结果排序变成
猪啊
猪啊
狗啊
狗啊
牛啊
牛啊
------解决思路----------------------
猪,狗,牛是在同一字段中的,楼上的不行。

可以这样写,例如字段名叫shengxiao

<br />$sqlstr = "select *,IF(shengxiao='猪啊',1, IF(shengxiao='狗啊',2,3)) as s from table order by s";<br />
Copy after login

------解决思路----------------------
create temporary table T<br />  select '猪啊' as f<br />  union select '牛啊'<br />  union select '狗啊';<br /><br /><br />select *, if(locate('猪', f), '猪',if(locate('牛',f),'牛', if(locate('狗',f),'狗',''))) as x <br /> from T order by find_in_set(x, '猪,狗,牛')
Copy after login
f    x <br />猪啊 猪 <br />狗啊 狗 <br />牛啊 牛 <br />
Copy after login

Related labels:
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