避免按MYSQL IN关键字排序 [英] avoid Sorting by the MYSQL IN Keyword

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

问题描述

当在数据库中查询一组ID时,mysql不按照ID的指定顺序提供结果.我正在使用的查询如下:

When querying the db for a set of ids, mysql doesnot provide the results in the order by which the ids were specified. The query i am using is the following:

SELECT id ,title, date FROM Table WHERE id in (7,1,5,9,3)

返回的结果是顺序为1,3,5,7,9.

in return the result provided is in the order 1,3,5,7,9.

如何避免这种自动排序

推荐答案

如果要按id中指定的顺序按id对结果进行排序,则可以使用FIND_IN_SET作为:

If you want to order your result by id in the order specified in the in clause you can make use of FIND_IN_SET as:

SELECT id ,title, date 
FROM Table 
WHERE id in (7,1,5,9,3)
ORDER BY FIND_IN_SET(id,'7,1,5,9,3')

这篇关于避免按MYSQL IN关键字排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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