mysql模糊查询通过排序调整精准值

背景:通过模糊查询到的结果,精准值排在后面

解决:通过调整字段排序

select * from user where 1=1
<if test="keyword != null and keyword != ''">
    and name like concat('%', #{keyword}, '%')
</if>
<if test="sort == 1 and (keyword == null or keyword == '')">
            ORDER BY status DESC, id
 </if>
<if test="sort == 1 and keyword != null and keyword != ''">
            ORDER BY (name = #{keyword}) DESC, status DESC, id
</if>
limit #{offset},10;

这条SQL语句首先使用了Like查询操作符查询出所有包含keyword字符的信息,然后使用Order By对查询结果进行排序。排序方式是先按照“是否包含精确匹配的结果”进行排序(使用DESC关键字是为了将包含“keyword”的结果排在前面),然后按照stauts,id进行排序。

posted @ 2024-09-03 17:14  白玉神驹  阅读(64)  评论(0编辑  收藏  举报