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..

Leave a Reply

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