group_concat 不适用于计数

group_concat doesn't work with count

我用

SELECT 
GROUP_CONCAT(DISTINCT `a`.`IDperson` SEPARATOR ', ') AS `person`,
COUNT(`a`.`IDjobs`) AS `total`
FROM
`a`
GROUP BY `a`.`ID_person`
ORDER BY `total` DESC

我需要的是返回类似

的结果
person        total
 2342           98
 1342           75
 3844           70
 1705           62
 3309           53
 5918, 1328     52
 1503, 1890     46
21004, 6536     45

但是没用 它返回的结果就像 GROUP_CONCT 不工作

    person        total
     2342           98
     1342           75
     3844           70
     1705           62
     3309           53
     5918           52
     1328           52
     1503           46
     1890           46
    21004           45
     6536           45

似乎你需要根据计数的数量进行 concat grouing,所以你应该

select GROUP_CONCAT(DISTINCT t.IDperson SEPARATOR ', ') AS person,  t.total
from (
      select DISTINCT a.IDperson as IDPerson, COUNT(a.IDjobs) AS `total`
      FROM a
      GROUP BY a.ID_person ) t 
group by t.total
ORDER BY t.total DESC

我推测你想要:

SELECT numjobs, GROUP_CONAT(idperson SEPARATOR ', ' ORDER BY idperson) as persons
FROM (SELECT idperson, COUNT(*) as numjobs
      FROM a
      GROUP BY idperson
     ) ap
GROUP BY numjobs
ORDER BY numjobs DESC;

GROUP_CONCAT() 完美运行。因为你 GROUP BY `a`.`ID_person`,每组只包含一个 `a`.`ID_person` 的值,因此你得到的结果。你可能想要 `GROUP BY `a`.`IDjobs`:

SELECT 
    GROUP_CONCAT(DISTINCT `a`.`IDperson` SEPARATOR ', ') AS `person`,
    COUNT(`a`.`IDjobs`) AS `total`
FROM
   `a`
GROUP BY `a`.`IDjobs`
ORDER BY `total` DESC