Mysql - 如何通过交替(1,2,3,1,2,3,1,2,3,)行对结果进行排序,这可能吗?

发布于 2024-08-15 22:28:49 字数 466 浏览 6 评论 0原文

我想按客户端 1、2、3 对结果进行排序,然后再按客户端 1、2、3,依此类推。

有没有办法在不使用 for 循环或进行三个单独查询的情况下执行此操作?不仅如此,我正在处理分页数据,因此它需要返回 x 个结果,但要一致。

有什么想法吗?也许可以分组?

client_id  project_id  project_name  
---------- ----------  ------------
 1         42          project abc
 2         49          project xyz
 3         41          project 123
 1         22          project apple
 2         29          project orange
 3         21          project banana

I want to order my results by client 1, 2, 3, then again client 1, 2, 3, and so on.

Is there a way to do this without using a for loop or making three separate queries? Not only that, but I am working with paginated data so it needs to return x results, but consistently.

Any ideas? GROUP BY maybe?

client_id  project_id  project_name  
---------- ----------  ------------
 1         42          project abc
 2         49          project xyz
 3         41          project 123
 1         22          project apple
 2         29          project orange
 3         21          project banana

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

稀香 2024-08-22 22:28:49

使用:

SELECT x.client_id, 
       x.project_id,
       x.project_name
  FROM (SELECT t.client_id,
               t.project_id,
               t.project_name,
               CASE
                 WHEN @client_id != t.client_id THEN @rownum := 0
                 WHEN @client_id = t.client_id THEN @rownum := @rownum + 1
                 ELSE @rownum 
               END AS rank,
               @client_id := t.client_id
          FROM TABLE t,
               (SELECT @rownum := 0, @client_id
      ORDER BY t.client_id) r) x
ORDER BY x.rank, x.client_id

MySQL没有任何排名功能,但幸运的是你可以使用变量。关键是当 client_id 与之前的 client_id 不匹配时重置 @rownum 值 - 子查询中的 ORDER BY 是为了确保客户端按顺序排列。

Use:

SELECT x.client_id, 
       x.project_id,
       x.project_name
  FROM (SELECT t.client_id,
               t.project_id,
               t.project_name,
               CASE
                 WHEN @client_id != t.client_id THEN @rownum := 0
                 WHEN @client_id = t.client_id THEN @rownum := @rownum + 1
                 ELSE @rownum 
               END AS rank,
               @client_id := t.client_id
          FROM TABLE t,
               (SELECT @rownum := 0, @client_id
      ORDER BY t.client_id) r) x
ORDER BY x.rank, x.client_id

MySQL doesn't have any ranking functionality, but luckily you can use variables. The key was resetting the @rownum value when the client_id doesn't match the previous client_id - the ORDER BY in the subquery is to ensure that clients are in order.

疧_╮線 2024-08-22 22:28:49

为什么不ORDER BY id

Why not ORDER BY id?

习ぎ惯性依靠 2024-08-22 22:28:49

GROUP BY 不会有帮助。这是否可能在很大程度上取决于您的数据。基本上,您需要一个复杂的 ORDER BY 来根据其他值将某些内容组合在一起。

例如,使用您提供的示例数据,您可以使用:

ORDER BY FLOOR(project_id / 10), client_id

这对您的真实数据不太可能有用,但它给出了您需要采用单独字段的想法,使该数据的某些子集达到相同目的排序(在本例中,所有具有相同 10 秒值的内容)并且对 client_id 进行二次排序。

虽然这在很大程度上取决于最终的公式,但像这样的东西应该是一个相对稳定的排序,因此添加分页,例如via

LIMIT 10, 10

应该返回一致的结果。

当然,这样做意味着您不会从索引中获得任何好处,因此如果您有很多行,它最终会比使用循环/单独查询执行某些操作要慢。

GROUP BY is not going to be helpful. Whether this is possible is going to depend a lot on your data. Basically you would need a convoluted ORDER BY that hacked something together based on other values.

For instance, using the example data you gave, you could use:

ORDER BY FLOOR(project_id / 10), client_id

This is unlikely to be useful on your real data but it gives the idea that you would need to take a separate field, make some subsets of that data be equivalent for the purpose of sorting (in this case, everything with the same 10s value) and the have a secondary sort on client_id.

Although it depends a lot on the final formula, something like this should be a relatively stable sort, so adding pagination, such as via

LIMIT 10, 10

should return consistent results.

Of course, doing this means you get no benefits from having indexes so if you have a lot of rows, it will end up being slower than doing something with loops/separate queries.

两个我 2024-08-22 22:28:49

除非我看错问题了,这就是你想要的吗?

SELECT *
FROM table
WHERE client_id in (1, 2, 3)
ORDER by id, client_id

Unless I read the question wrong is this what you want?

SELECT *
FROM table
WHERE client_id in (1, 2, 3)
ORDER by id, client_id
永言不败 2024-08-22 22:28:49

听起来像是需要在客户端完成的事情。

Sounds like something to be done client-side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文