2

我想根据查询的包含对此查询的结果进行排序:

这里是 :

SELECT a from Frais a where a.libelle = 'FRET' or a.libelle = 'Douane' or a.libelle = 'Transitaire'

我想要有FRET第一和Douane之后的记录等等

order by libelle 

不能解决它根据字母顺序 asc 或 desc 对它们进行排序的问题

4

3 回答 3

4

一种选择是使用CASE语句:

SELECT * 
FROM Frais a 
WHERE a.libelle = 'FRET' 
   OR a.libelle = 'Douane' 
   OR a.libelle = 'Transitaire'
ORDER BY 
   CASE 
     WHEN a.libelle = 'FRET' THEN 1
     WHEN a.libelle = 'Douane' THEN 2
     WHEN a.libelle = 'Transitaire' THEN 3
   END
于 2013-05-07T17:52:20.677 回答
3
SELECT a from Frais a where a.libelle = 'FRET' or a.libelle = 'Douane' or a.libelle = 'Transitaire'
order by case a.libelle when 'FRET' then 0 when 'Douane' then 1 when 'Transitaire' then 2 end
于 2013-05-07T17:53:02.793 回答
0

您也可以通过将值放在具有显式排序的“临时”表中来做到这一点:

select f.*
from frais f join
     (select 'FRET' as val, 1 as ord union all
      select 'Douane', 2 union all
      select 'Transitaire', 3
     ) vals
     on f.libelle = vals.val
order by vals.ord
于 2013-05-07T17:55:56.957 回答