How To Order Data Inside GROUP_CONCAT Using MySQL

How To Order Data Inside GROUP_CONCAT Using MySQL

I use GROUP_CONCAT function in MySQL quite often, but today I encountered a situation where by I need to order the data returned from the GROUP_CONCAT function call. Simply having “ORDER BY” clause at the end of the query does not help as it orders the final output rows, not the data inside the GROUP. I have found the way to do it, very simple:
SELECT `column`, GROUP_CONCAT(DISTINCT choice_id ORDER BY bit_index DESC) AS choice_ids FROM table_bit_index GROUP BY `column`
The stuff inside the GROUP_CONCAT does the trick “DISTINCT choice_id ORDER BY bit_index DESC“. Now I know..

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *

My new Snowflake Blog is now live. I will not be updating this blog anymore but will continue with new contents in the Snowflake world!