mysqlsqlsql-order-bysql-query-store

Ordering columns in sql


I have data in DB


Germany
India
USA
China
Japan
Africa


I want a sql query which results:


India (India as first row)
Africa (all the other alphabetically arranged)
Japan
USA
China (China as last row)



Solution

  • You can use several levels of sorting:

    order by col = 'India' desc, col = 'China', col
    

    Rationale: in MySQL, expression col = <val> returns 1 if the condition is fulfilled, else 0. So col = 'India' desc puts India first, while col = 'China' puts China last. Ties are then broken with a regular sort on the country name.