说我有以下结果:
Mackay
Mackay Airport
Melbourne
Melbourne Airport
Sydney
Sydney Ac
Sydney Airport
我怎样才能让他们订购机场总是在他们的顶部?按字母顺序排列,例如:
Mackay Airport
Mackay
Melbourne Airport
Melbourne
Sydney Airport
Sydney
Sydney Ac
对如何使机场更加突出有点困惑。
答案 0 :(得分: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
答案 1 :(得分:1)
似乎您不能同时按整数和字符串排序,但您可以根据是否存在“Airport”来派生要排序的字符串。这很愚蠢,但它有效:
ORDER BY IF(name NOT LIKE '% Airport', CONCAT(name, ' Birport'), name)
答案 2 :(得分: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;
以下是示例。