Good day! There is a table of activities id, type, source_id; It has the type types: Image, Progress, Program, Profile. You need to make a request so that the grouping is only for one type Image, and the rest Progress, Program, Profile are not grouped. Those. there is:

source_id type 1 Image 2 Image 3 Image 4 Program 5 Profile 6 Program 7 Profile 

Need to get:

  source_id type source_ids 1 Image 1,2,3 4 Program 4 5 Profile 5 6 Program 6 7 Profile 7 

Thank!

  • one
    And UNION will do? SELECT *, GROUP_CONCAT (source_id) AS source_ids FROM table_name WHERE type = 'Image' UNION SELECT *, source_id AS source_ids FROM table_name WHERE type <> 'Image'; - BOPOH
  • In principle, also an option. Thanks - idd

1 answer 1

 SELECT *, CASE WHEN (`user_activity`.`type` = 'AddImage') THEN GROUP_CONCAT(DISTINCT `user_activity`.`source_id` ORDER BY `user_activity`.`created_at` ASC SEPARATOR ', ') ELSE `user_activity`.`id` END ids FROM `user_activity` GROUP BY type , CASE WHEN (type = 'AddImage') THEN `user_activity`.`user_id` ELSE `user_activity`.`id` END