SQL對於AAA,AA+,AA,AA-,A+,A,A-,B+…排序
現象
在sql 中無論是字典序還是用分別查出來進行拼接到一起,效果實現都不是很理想。
解決方法
使用一張額外的字典表,對應的各等級都加上順序。
與你需要的表進行 join 聯合查詢即可。
以mysql爲例進行舉例:
SELECT * FROM `test` left join dict on test.level = dict.`name` ORDER BY dict.`value` asc
you get it
第二種方法
這種方法也就不需要用字典表了
order by case
when graderesult='AAA' then 0
when graderesult='AA+' then 1
when graderesult='AA' then 2
when graderesult='AA-' then 3
when graderesult='A+' then 4
when graderesult='A' then 5
when graderesult='A-' then 6
when graderesult='B+' then 7
when graderesult='B' then 8
when graderesult='B-' then 9
when graderesult='C' then 10
desc