Skip to content
Advertisement

Order SQL results with sticky item

Is there a way to order results alphabetically and by country code, but making one result stick at the top for each country code?

For example, this order:

would currently display as;

I’d like to be able to order the results first by country (GB first), then for GB have Oxford at the top of the list, with any additional result being alphabetical.

The FR results should start with Paris, also with any additional result being alphabetical.

Is this a possibility without further order data being added to the DB?

Advertisement

Answer

You want conditional order by :

If you want oxford at top for only GB country then add one more condition :

Note : This will first desc by country not only for GB & then sort first oxford location.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement