Say I have the following results:
Mackay
Mackay Airport
Melbourne
Melbourne Airport
Sydney
Sydney Ac
Sydney Airport
How can I make it so they are ordered with Airport always at the top of them? Alphabetically, eg:
Mackay Airport
Mackay
Melbourne Airport
Melbourne
Sydney Airport
Sydney
Sydney Ac
A bit confused on how to make the Airports more prominent.
Not sure if the following query covers all the cases, but it seems to work with your sample data:
select name,
SUBSTRING_INDEX(name,'Airport',1)
as l,
LOCATE('Airport',name) as r from
(
select 'Sydney Airport' as name
union all
select 'Sydney'
union all
select 'Sydney Ac'
union all
select '
Mackay Airport'
union all
select 'Mackay'
union all
select 'Melbourne'
union all
select 'Melbourne Airport'
)a
order by l asc, r desc
with table it will look like
select name
from table1
ORDER BY SUBSTRING_INDEX(name,'Airport',1) ASC,
LOCATE('Airport',name) DESC
It seems that you can't order by integer and string simultaneously, but you can derive the string to order by based on whether "Airport" is present. It's silly, but it works:
ORDER BY IF(name NOT LIKE '% Airport', CONCAT(name, ' Birport'), name)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With