GROUP_CONCAT不计
工作,我用GROUP_CONCAT不计
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
看起来你需要按照计数的数量来分组,所以你应该使用
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
没有它不起作用。它返回像 人\t 11837,16140,17081,17084,17085,17087,20032,27849,30414,31470,无与伦比的数据31891 \t 共有11 –
表看起来像你没有在问题描述, IDjobs和IDperson字段之间的关系是什么,以及聚合规则是什么。鉴于问题没有明确定义,只要它在'person'列中产生聚合值,任何答案都可以工作。如果您需要符合您实际需求的答案,请更好地描述需求。 – axiac
您是否收到任何错误? – RohitS
编辑您的问题并提供样本数据和期望的结果。 –
['GROUP_CONCAT()'](https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat)完美地工作。由于你的GROUP BY a.ID_person',每个组只包含一个'a.ID_person'的值,因此你得到的结果。你可能想要“GROUP BY”其他列。 – axiac