sql - MySQL group_concat() ordering by case statement values -


in mysql group_concat() clause, i'm trying order resulting values of case statement. following query configuration orders things.name not order 'non-us' or 'unknown' values within same context.

select    things.id   ,group_concat(distinct      case     when things.name <> 'united states' 'non-us'     when things.name null 'unknown'     else things.name     end   order name separator ', ') things group things.id 

i want this, it's not working:

select    things.id   ,group_concat(distinct      (case     when things.name <> 'united states' 'non-us'     when things.name null 'unknown'     else things.name     end) new_name   order new_name separator ', ') things group things.id 

is there way sort "new_name" without using sub-queries/ nested queries?

you can accomplish ordering column position instead of column name.

for case order 1 should work.

select    things.id   ,group_concat(distinct      case     when things.name <> 'united states' 'non-us'     when things.name null 'unknown'     else things.name     end   order 1 separator ', ') things group things.id 

Comments

Popular posts from this blog

java - HTTP Status 500 - An exception occurred processing JSP page /second.jsp at line 15 -

SQL Server - MyCTE query based on 24 hour period (next day) -

php - Using str_replace to translate a MySQL Table in html -