如何用“优、良、中、差”来排序(100分)

  • 主题发起人 主题发起人 hehuan
  • 开始时间 开始时间
H

hehuan

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,order by 只能是字母排序,但我想用某种特定的顺序来排序,该如何写?非常感谢!
 
加一个辅助字段是最方便的,如:为优时辅助字段值为A,以此类推,排序时按辅助字段排就行了.
或者:数据库中根本就不存储优良中差,而是存储ABCD,显示的时候调整为优良中差
这是笨办法,看高手们的吧.
 
--1.可以用楼上的方法,

--2.可以用下面这样(bbb是字段名)
select * from Table1
order by (case bbb
when '优' then 1
when '良' then 2
when '中' then 3
when '差' then 4
else 0
end)

--3.也可以这样(bbb是字段名)
select * from Table1
order by charindex(ltrim(rtrim(bbb)),'优良中差')
 
方法2比较好,不做别的改动.思路也清晰
 
select * from Table1
order by (case bbb
when '优' then 1
when '良' then 2
when '中' then 3
when '差' then 4
else 0
end)
 
order by + case (另做字段)
 
select * from utable
order by
decode(bbb,'优',1,'良',2,'中',3,'差'4,9999) as sortindex

oracle 数据库下
 
select * from table_score
orderby(case
when score>=85 then '优'
when score>=70 then '良'
when score>=60 then '中'
else
'差'
end
 
优、良、中、差
拼音不对 Y、L、Z、C
CLYZ 或 ZYLC
 
从前的例子
表:tbl1
season

秋 <-----汉字


排序:按“春夏秋冬”自然顺序。
select * from tbl1
order by iif(season='春',1,iif(season='夏',2,iif(season='秋',3,4)))

SELECT T1.*
FROM tb1 AS T1, [Select 1 as Idx,'春' as season from (Select Count(*) from tb1) K1
union all
Select 2 as Idx,'夏' as season from (Select Count(*) from tb1) K1
union all
Select 3 as Idx,'秋' as season from (Select Count(*) from tb1) K1
union all
Select 4 as Idx,'冬' as season from (Select Count(*) from tb1) K1
]. AS T
WHERE T1.season=T.season
ORDER BY T.Idx;
 
后退
顶部