Using COUNT in GROUP_CONCAT

前端 未结 5 1032
陌清茗
陌清茗 2021-02-19 12:04

This is my table:

  id  | fk_company
-------------------
  1   |     2    
  2   |     2    
  3   |     2    
  4   |     4    
  5   |     4    
  6   |     11          


        
5条回答
  •  南旧
    南旧 (楼主)
    2021-02-19 12:29

    You need to COUNT() with GROUP BY in an inner SELECT clause first and then apply GROUP_CONCAT();

    SELECT GROUP_CONCAT(cnt) cnt
    FROM (
        SELECT COUNT(*) cnt
        FROM table1
        GROUP BY fk_company
    ) q;
    

    Output:

    |   CNT   |
    -----------
    | 3,2,3,1 |
    

    Here is SQLFiddle demo.

提交回复
热议问题