自定义mysql按字段值排序 [英] Custom mysql sort by field value

查看:390
本文介绍了自定义mysql按字段值排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对mysql排序有疑问.我有一个数据库字段,其中包含这样的1,6,3,8,4数据.我分解了该字段值,并针对另一个表进行了自定义查询:WHERE id='1' or id='6' or id='3'....

I have a question about mysql sorting. I have a database field with data like this 1,6,3,8,4. I exploded this field value and made custom query for another table: WHERE id='1' or id='6' or id='3'....

它工作正常,但结果为ASC by id.有没有一种方法可以使结果以与查询相同的顺序出现?

It works fine BUT the result is ASC by id. Is there a way to make the result appear at same order as the query?

推荐答案

使用ORDER BY子句中的> FIELD() 函数:

Use the FIELD() function in the ORDER BY clause:

ORDER BY FIElD(id, '1', '6', '3', ...);

在此处查看其运行情况:

See it in action here:

这篇关于自定义mysql按字段值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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