order by 进行排序
默认升序(从小到大) asc
根据年龄进行排序。
select * from students order by age;
降序 (desc)
根据年龄进行排序 — 降序;
select * from students order by age desc;
按照成绩进行从大到小的排序;
select * from students order by score desc;
按照成绩进行从小到达的排序;
select * from students order by score asc;
条件过滤,排序
使用where 可以对查询的结果进行内容过滤。
如果既要过滤内容,又要进行排序。
语法
查询 所有男同学信息 按照age 升序排序;
select * from students
where sex = "男" -- 条件
order by age asc; -- 排序
查询 20岁以上的男生, 按照成绩排序;
select * from students
where age>20 and sex='男'
order by score;
查询 20岁-50岁的男生,并按照年龄从大到小排序;
select * from students
where age between 20 and 50 and sex='男'
order by age desc;
查询 年龄不为Null的所有学生,并按照成绩进行排序;
select * from students
where not age is null
order by score;
多个字段排序
在给学生进行排序的时候,要实现如下需求
如果 age 相等, 按照 成绩进行排序。
order by age, score
- 查询学生表所有信息,按照年龄进行排序,如果年龄一样,按照成绩进行排序;
select * from students
order by age,score;
当age 一样的时候,才会按照成绩进行排序;
order by age asc,score desc
- 按照年龄从升序排序,如果年龄一样,再按照成绩进行降序排序(成绩越大,排在越前)
select * from students
order by age asc, score desc;
所有男同学,按照age 排序,如果age 一样按照 score 排序;
select * from students
where sex='男'
order by age asc, score asc;
所有姓名为3个字的男同学, 按照score 排序,如果score 一样,按照age 倒序排序;
select * from students
where username like "___" and sex="男"
order by score, age desc;
所有语文成绩及格的男生,按照age 倒序排序;
select * from students
where course="语文" and sex="男" and score >= 60
order by age desc;