MySQL通过ID指定任意顺序 [英] MySQL specify arbitrary order by id

查看:96
本文介绍了MySQL通过ID指定任意顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为MySQL SELECT语句指定任意顺序?例如,

Is it possible to specify an arbitrary order for a MySQL SELECT statement? E.g.,

SELECT * FROM table_name WHERE id IN (1, 3, 2, 9, 7) ORDER BY (1, 3, 2, 9, 7);

直接在IN之后列出的数字顺序似乎无关紧要.

The order of the numbers listed directly after IN do not seem to matter.

推荐答案

FIND_IN_SET函数可以解决问题

FIND_IN_SET function will do the trick

SELECT * FROM table_name WHERE id IN (1, 3, 2, 9, 7) ORDER BY FIND_IN_SET(id, '1,3,2,9,7');

http://dev. mysql.com/doc/refman/5.0/zh-CN/string-functions.html#function_find-in-set

请注意find_in_set函数的字符串参数中缺少空格.

Note the lack of spaces in the string argument of the find_in_set function.

这篇关于MySQL通过ID指定任意顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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