MySQL - SELECT ... WHERE id IN (..) - 正确的顺序
我有以下查询
SELECT * FROM table WHERE id IN (5,4,3,1,6)
,我想按照 id in.. 中指定的顺序检索元素,这意味着它应该返回:
5 ....
4 ....
3 ....
1 ....
6 ....
关于如何执行此操作的任何想法?
I have the following query
SELECT * FROM table WHERE id IN (5,4,3,1,6)
and I want to retrieve the elements in the order specified in the id in..
, meaning it should return:
5 ....
4 ....
3 ....
1 ....
6 ....
Any ideas on how to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 FIELD():
Use FIELD():
如果有人仍在搜索,我刚刚找到了它。
您可以找到该函数的参考此处
In case anyone is still searching I just found it..
And a reference for the function you can find HERE
那么你必须为每个 id 创建一个 Id,所以:
id |顶部
1 = 5
2 = 4
3 = 3
4 = 1
6 = 6
使用 IN 语句仅查看这些值是否在列表中,而不以任何特定顺序对它们进行排序
Well your going to have to create a Id for each of the id's so:
id | otherid
1 = 5
2 = 4
3 = 3
4 = 1
6 = 6
using the IN STATEMENT only looks to see if those values are in the List, doesnt order them in any specific order