6

假设我有以下结果:

Mackay
Mackay Airport
Melbourne
Melbourne Airport
Sydney
Sydney Ac
Sydney Airport

我怎样才能做到让他们总是在他们的顶部订购机场?按字母顺序,例如:

Mackay Airport
Mackay
Melbourne Airport
Melbourne
Sydney Airport
Sydney
Sydney Ac

对如何使机场更加突出有点困惑。

4

3 回答 3

2

不确定以下查询是否涵盖所有情况,但它似乎适用于您的示例数据:

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

用桌子看起来像

select name
from table1 
ORDER BY SUBSTRING_INDEX(name,'Airport',1) ASC,
LOCATE('Airport',name) DESC
于 2013-02-20T00:23:06.480 回答
1

似乎您不能同时按整数和字符串排序,但您可以根据“机场”是否存在来派生要排序的字符串。这很愚蠢,但它有效:

ORDER BY IF(name NOT LIKE '% Airport', CONCAT(name, ' Birport'), name)
于 2013-02-20T00:32:05.070 回答
0

以下查询将为您提供预期的输出(即使查询很复杂)。

SELECT t.name
FROM (
SELECT @num := IF(@num IS NULL, 1, @num+1) AS number, name
FROM places
ORDER BY IF ((name LIKE '% Airport'), SUBSTRING(name, 1, LOCATE(' Airport', name) - 1), name)     DESC) t
ORDER BY t.number DESC;

这是示例。

http://www.sqlfiddle.com/#!2/117b7/80

于 2013-02-20T03:08:03.017 回答