自定义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: