单表查询中的order by :
作用:选定字段进行升序或降序排列
首先创建表:
create table student(
id char(36) primary key,
name varchar(8) not null,
age int(3) default 0,
mobile char(11),
address varchar(150)
)
insert into student
values ('9b4435ec-372c-456a-b287-e3c5aa23dff4','张三',24,'12345678901','北京海淀');
insert into student
values ('a273ea66-0a42-48d2-a17b-388a2feea244','李%四',10,'98765432130',null);
insert into student
values ('eb0a220a-60ae-47b6-9e6d-a901da9fe355','张李三',11,'18338945560','安徽六安');
insert into student
values ('6ab71673-9502-44ba-8db0-7f625f17a67d','王_五',28,'98765432130','北京朝阳区');
insert into student
values ('0055d61c-eb51-4696-b2da-506e81c3f566','王_五%%',11,'13856901237','吉林省长春市宽平区');
怎样使用:
形式:order by [字段名] asc(升序)/desc(降序)
例如我们要按照年龄对数据进行将序排列:
select * from student order by age desc
对于不使用desc或asc说明的,默认是升序
select * from student order by age
对多个字段排序时:
select * from student order by age desc,mobile desc
含义是先按照age降序排列,当age相同再对mobile将序排列:
asc与desc跟在每个字段后面,没有则同样是默认asc