Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering MySQL string results without a defined Collation?

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.

like image 268
Latox Avatar asked Feb 20 '13 00:02

Latox


2 Answers

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
like image 59
a1ex07 Avatar answered Nov 13 '22 00:11

a1ex07


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)
like image 21
Explosion Pills Avatar answered Nov 13 '22 00:11

Explosion Pills