MySQL 按 IN 子句中相同的值顺序排序 [英] MySQL order by the same order of values in IN clause

查看:58
本文介绍了MySQL 按 IN 子句中相同的值顺序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是带有 IN 子句的简单查询.但问题是我需要按照 id 的相同顺序获取输出.

Here is the simple query with IN clause. But the problem is i need to get the output in the same order of the ids.

SELECT GROUP_CONCAT(username) as users FROM usertable WHERE usr_id IN (54,68,46)

例如,如果我通过 54,68,46,那么 usr_id 54 的行应该首先出现,然后是 68,然后是 46.有什么办法可以在 MySQL 中实现这一点吗?

For example if i pass 54,68,46 then the row with usr_id 54 should come first then 68 and then 46 should come. Is there any way to achieve this in MySQL?

推荐答案

我们可以在 order by 子句上使用 FIND_IN_SET 来获得相同顺序的值,就像这样.

We can use FIND_IN_SET on order by clause to get the values in same order like this.

SELECT `username` as users FROM usertable WHERE usr_id IN (54,68,46) ORDER BY FIND_IN_SET(`usr_id`,"54,68,46")

但我不知道如何以相同的顺序GROUP_CONCAT.如果有人用这种简单的方法给出答案,我可以接受这个答案.

But i don't know how to GROUP_CONCAT in the same order. If anybody gives the answer for that in these kind of simple approach, i can accept that answer.

这篇关于MySQL 按 IN 子句中相同的值顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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