假如我要对以下sql查询语句的查询结果进行排序(两个字段a,b)。

select  a,b,c,d,e  from  table_name;

1.先按照a升序,再按照b降序

select  a,b,c,d,e  from  table_name  order  by a,b desc;

也可以写为(因为默认升序,可以不写):

select  a,b,c,d,e  from  table_name  order  by  a asc,b desc;

2.先按照a升序,再按照b升序

select  a,b,c,d,e  from  table_name  order by a,b;

也可以写为:

select  a,b,c,d,e  from  table_name  order by  a asc,b asc;

3.先按照a降序,再按照b升序

select  a,b,c,d,e  from  table_name  order by  a desc,b;

也可以写为:

select  a,b,c,d,e  from  table_name  order  by  a desc,b asc;

4.先按照a降序,再按照b降序

select  a,b,c,d,e  from  table_name  order by  a desc,b desc;

注:都降序必须写两个desc,因为排序不写默认asc。

 

Logo

更多推荐