我正在尝试执行一个 mysql 查询,它将使用以“timberwood”开头的标题对我的结果进行排序,首先开始,然后从那里开始按字母顺序排列。这可能吗?或者我是否需要以某种方式在 php 中重新组织它?这是我尝试使用的代码,但它没有按我想要的方式排序。
SELECT *, (case when `title` like "timberwood%" then substring(title,6) else title end) as testing FROM #__downloads_items WHERE state = 1 ORDER BY testing asc
如有任何帮助,我们将不胜感激!
最佳答案
这应该可以解决:
Order by case when title like 'timberwood%' then 0 else 1 end asc, title asc
“case when”将为以“timberwood%”开头的任何内容提供 0 值,然后为其他任何内容提供 1 值。由于这是排序的第一件事,所有“timberwood%”结果都应该排在最前面。
关于php - mysql order by,从一个单词开始,然后按字母顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28207629/