Using COUNT in GROUP_CONCAT

前端 未结 5 1796
执笔经年
执笔经年 2021-02-19 12:29

This is my table:

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


        
5条回答
  •  生来不讨喜
    2021-02-19 12:37

    You can also achieve that by counting the number of commas (or whatever's your separator) in the GROUP_CONCAT:

    SELECT (LENGTH(GROUP_CONCAT(DISTINCT fk_company))-LENGTH(REPLACE(GROUP_CONCAT(DISTINCT fk_company), ',', '')))
    FROM `table`
    GROUP BY fk_company
    

提交回复
热议问题