数据库技术,mysql,oracle
oracle:字段将指定值的数据排在最前或最后
1.使用order by decode方法.
decode(字段或字段的运算,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值);
需求:将type_id(非0)值是8或者9的课程排在最前面;
select * from course order by order by decode(type_id,8,0,9,0) asc,create_date desc
以上把8和9转换为0,从小到大排序就能实现排在最前面;
如果9要在8前面,那么写成decode(type_id,8,1,9,0)。8变成1就变成0(此时需要保证没有原始type_id=1的数据)。
2.使用replace方法(有些问题)
order by replace(type_id,8,0) 8变为0则会排在最前。
使用replace貌似存在问题,8替换为0,那么18会被替换成10,会影响其他值的排序