简体   繁体   English

在group_concat中排序

[英]Sorting in group_concat

Data: 数据:

id  uid     type

1   20      A
2   20      B
3   20      A
4   6       A
5   1       A
6   3       A
7   6       A
8   1       B

Scenario: 场景:

I want to group by type and sort it by id . 我想按type分组并按id排序。 I am using group by to group the uid . 我正在使用group by来分组uid

Current Query: 当前查询:

SELECT
    type,
    GROUP_CONCAT(DISTINCT uid) AS users,
    COUNT(type) AS typeCount
FROM
    `test2`
GROUP BY
    type

Problem: 问题:

But the order of the uid is incorrect, it should be in descending order according to id . 但是uid的顺序不正确,它应该按照id降序排列。

Expected Result: 预期结果:

type    users       typeCount
A       6,3,1,20    6
B       1,20        2

My results: 我的结果:

type    users       typeCount
A       20,6,1,3    6
B       20,1        2

The mistery of MySQL. MySQL的谜团。

Actually the engine takes first value in ASC order, no matter that you are asking for DESC by ID, so first "flip" the table, then: 实际上引擎以ASC顺序获取第一个值,无论你是通过ID要求DESC,所以首先“翻转”表,然后:

SELECT
    type,
    GROUP_CONCAT(DISTINCT uid ORDER BY id DESC) AS users,
    COUNT(type) AS typeCount
FROM
    (SELECT * FROM `test2` ORDER BY id DESC) test2
GROUP BY
    type

SQLFiddleDemo SQLFiddleDemo

You can do something like Sampson suggested in this post: 你可以做一些像Sampson在这篇文章中建议的:

MySQL: Sort GROUP_CONCAT values MySQL:排序GROUP_CONCAT值

Here is link to MySQL docs 这是MySQL文档的链接

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function%5Fgroup-concat http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function%5Fgroup-concat

Here is the example he given: 这是他给出的例子:

SELECT student_name,
  GROUP_CONCAT(DISTINCT test_score ORDER BY test_score DESC SEPARATOR ' ')
  FROM student
  GROUP BY student_name;

You just need to adjust it to your needs. 您只需根据需要进行调整即可。

Hope this helps 希望这可以帮助

The answer from @mitkosoft is already right. @mitkosoft的答案已经是正确的。

I am posting this just to analyze the right expected result. 我发布此信息只是为了分析正确的预期结果。

From the following output, we can see that, for type 'A' group, before DISTINCT taking effect, after ORDER BY id DESC, the rows are: 从下面的输出中,我们可以看到,对于类型'A'组,在DISTINCT生效之前,在ORDER BY id DESC之后,行是:

6 3 1 6 20 20 6 3 1 6 20 20

Then DISTINCT can produce two possible results: 6,3,1,20 or 3,1,6,20. 然后DISTINCT可以产生两种可能的结果:6,3,1,20或3,1,6,20。

Which one is produced is undetermined and realization related. 产生哪一个是未确定的并且实现相关。 Otherwise, we can't rely on that. 否则,我们不能依赖于此。

Therefore, the expect result for group 'A' should be 6,3,1,20 or 3,1,6,20. 因此,'A'组的预期结果应为6,3,1,20或3,1,6,20。 Both correct. 两个都正确。

mysql> SELECT * FROM test2;
+------+------+------+
| id   | uid  | type |
+------+------+------+
|    1 |   20 | A    |
|    2 |   20 | B    |
|    3 |   20 | A    |
|    4 |    6 | A    |
|    5 |    1 | A    |
|    6 |    3 | A    |
|    7 |    6 | A    |
|    8 |    1 | B    |
+------+------+------+
8 rows in set (0.00 sec)

mysql> SELECT uid FROM test2 WHERE type='A' ORDER BY id DESC;
+------+
| uid  |
+------+
|    6 |
|    3 |
|    1 |
|    6 |
|   20 |
|   20 |
+------+
6 rows in set (0.00 sec)

Try something like: 尝试类似的东西:

 SELECT
    type,
    GROUP_CONCAT(DISTINCT uid) AS users,
    COUNT(type) AS typeCount
FROM
    (SELECT type, uid
     FROM `test2`
     ORDER BY uid desc) mytableAlias
GROUP BY
    type

No subquery is needed for this this. 这不需要子查询。 From your description you simply need an ORDER BY in the GROUP_CONCAT() : 根据您的描述,您只需在GROUP_CONCAT()使用ORDER BY

SELECT type,
        GROUP_CONCAT(DISTINCT uid ORDER BY uid DESC) AS users,
        COUNT(type) AS typeCount
FROM `test2`
GROUP BY type;

In MySQL, it is a good idea to avoid unnecessary subqueries, because the database engine materializes them. 在MySQL中,最好避免不必要的子查询,因为数据库引擎会实现它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM