w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
MySQL display results alphabetical order but show a particular item before the alphabetical sorted list

just do a conditional order by with a case statement like so

QUERY:

SELECT * 
FROM $tableName 
GROUP BY primary_category
ORDER BY 
    CASE primary_category WHEN 'USA' THEN 1 ELSE 2 END ASC,
    primary_category ASC

EDIT:

if you want to order by multiple fields first and then the rest you can do it like this.

SELECT * 
FROM $tableName 
GROUP BY primary_category
ORDER BY 
    CASE primary_category 
      WHEN 'USA' THEN 1 --#-- 1 for usa
      WHEN 'China' THEN 2 --#-- 2 for china
      ELSE 3 END ASC, --#-- 3 for anything else
    primary_category ASC




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.