- 排序
语法:
select * from 表名
order by 列1 asc | desc,列2 asc | desc,···
· 将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推
· 默认按照列值从小到大排序
· asc 从小到大排序,即升序
· desc 从大到小排序,即降序
例:
#查询删除男生学生信息,按学号降序
select * from students
where gender=1 and isdelete=1
order by id desc;
- 获取部分行
当数据量过大时,在一页中查看数据是一件非常麻烦的事情,所以我们可以用获取部分行
语法
select * from 表名
limit start,count;
例:
select * from students
limit 1,2;
· 从 start 开始,获取 count 条数据
· start 索引从0开始
示例:分页
· 已知:每页显示 m 条数据,当前显示第 n 页
· 求总页数:此段逻辑后面会在 python 中实现
查询总条数p1
使用p1除以m得到p2
如果整除则p2为总页数
如果不整除则p2+1为总页数
# 求第 n 页的数据
select * from students
where isdelete=0
limit (n-1)*m,m;