gpt4 book ai didi

mysql - 如何根据关键字对结果进行排序

转载 作者:可可西里 更新时间:2023-11-01 08:48:52 26 4
gpt4 key购买 nike

我正在开发一个分类网站。为了搜索产品或公司,我在前后都使用了通配符 (%)。

$keyword = "%".$user_keyword."%";

我正在使用以下 mysql 查询:

$query = "SELECT DISTINCT   login.id_user,  
login.company,
login.district,
FROM login LEFT JOIN products
ON login.id_user = products.id_user
WHERE (login.district LIKE ?
AND login.place LIKE ?)
AND
(login.company LIKE ?
OR login.summary LIKE ?
OR products.description LIKE ?)
LIMIT ?, ?";
";

这是有效的。但我想引用关键字对列表进行排序。例如:如果用户搜索“foo”,可能会得到以下结果:

fool
kafoo
safoora
foo
foolan

我想把“foo”作为第一项

最佳答案

请尝试以下操作:

$query = "SELECT DISTINCT   login.id_user,  
login.company,
login.district,
FROM login LEFT JOIN products
ON login.id_user = products.id_user
WHERE (login.district LIKE ?
AND login.place LIKE ?)
AND
(login.company LIKE ?
OR login.summary LIKE ?
OR products.description LIKE ?)
order by if(LOCATE('$user_keyword', login.district)=0, 9999, LOCATE('$user_keyword', login.district))+
if(LOCATE('$user_keyword', login.place)=0, 9999, LOCATE('$user_keyword', login.place))+
if(LOCATE('$user_keyword', login.company)=0, 9999, LOCATE('$user_keyword', login.company))+
if(LOCATE('$user_keyword', login.summary)=0, 9999, LOCATE('$user_keyword', login.summary))+
if(LOCATE('$user_keyword', products.description)=0, 9999, LOCATE('$user_keyword', products.description))
LIMIT ?, ?";
";

关于mysql - 如何根据关键字对结果进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19889958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com