2017-10-14 45 views
0

工作,我用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 
+0

您是否收到任何錯誤? – RohitS

+0

編輯您的問題並提供樣本數據和期望的結果。 –

+0

['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

回答

0

看起來你需要按照計數的數量來分組,所以你應該使用

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 
+0

它reponces與錯誤1248:每個派生的表必須有它自己的別名 –

+0

答案更新.. – scaisEdge

+0

現在它的話,因爲我需要。感謝的 –

0

我猜測,你想:

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; 
0

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 
+0

沒有它不起作用。它返回像 人\t 11837,16140,17081,17084,17085,17087,20032,27849,30414,31470,無與倫比的數據31891 \t 共有11 –

+0

表看起來像你沒有在問題描述, IDjobs和IDperson字段之間的關係是什麼,以及聚合規則是什麼。鑑於問題沒有明確定義,只要它在'person'列中產生聚合值,任何答案都可以工作。如果您需要符合您實際需求的答案,請更好地描述需求。 – axiac