如何根据一列对查询结果进行排序? [英] How can I sort the result of a query based on the one column?

查看:60
本文介绍了如何根据一列对查询结果进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的查询:

SELECT name, usage_guidance, total_used_num
FROM tags
WHERE
( name LIKE CONCAT('%', ?, '%') OR
  usage_guidance LIKE CONCAT(?, '%') ) AND
name NOT IN ($in)
LIMIT 6

现在我想按 name 列对结果进行排序.. 我的意思是我想把匹配的结果放在 name LIKE CONCAT('%', ?, '%') 条件在第一,然后其他结果应该在他们之后.我该怎么做?

Now I want to order the result by name column .. I mean I want to put the matched results because of name LIKE CONCAT('%', ?, '%') condition in first, and then other results should be after them. How can I do that?

推荐答案

您可以将 where 条件添加到 order by 子句中:

You would add the where conditions to the order by clause:

order by (name like CONCAT('%', ?, '%')) desc,
         (usage_guidance LIKE CONCAT(?, '%')) desc

MySQL 将数字上下文中的布尔表达式视为数字,1"表示真,0"表示假.因此,desc 排序顺序.

MySQL treats boolean expressions in a numeric context as numbers, with "1" for true and "0" for false. Hence the desc order for the sorts.

请注意,usage_guide 上的第二个条件并不是回答问题所必需的.

Note that the second condition on usage_guidance is not strictly necessary to answer the question.

这篇关于如何根据一列对查询结果进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆