.limit的用法
1.limit作用:将查询结果集的一部分取出来,通常使用在分页查询当中。
百度默认:一夜显示10条记录
分页的作用是为了提高用户的体验,因为一次全部都查出来,用户体验差,可以一页一页翻页看。

2.limit的用法
	完整用法:limit startIndex,length
		startIndex是起始下标,length是长度
		**起始下标从0开始**
	缺省用法:limit 5;这是取前5
	
	案例:按照薪资降序,取出排名在前5名的员工。
	select
		ename,sal
	from
		emp
	order by
		sal desc
	limit 5;//取前5

3.注意:mysql当中limitorder by之后执行!!!!!!
	案例1:取出工资排名在【3~5】名的员工
	select
		ename,sal
	from
		emp
	order by
		sal desc
	limit
		2,3;
	**2表示起始位置从下标2开始,就是第三条记录
	3表示长度**
	案例2:取出工资排名在【5~9】名的员工
	select
		ename,sal
	from
		emp
	order by
		sal desc
	limit
		4,5;

4.通用分页
每页显示3条记录
	第1页:limit 0,3   [0 1 2]2页:limit 3,3   [3 4 5]3页:limit 6,3   [6 7 8]4页:limit 9,3   [9 10 11]

**每页显示pageSize条记录
	第pageNo页:limit(pageNO - 1) * pageSize, pageSize**
		
	public static void main(String[] args){
		//用户提交过来一个页码,以及每页显示的记录条数
		int pageNo = 5;//第5页
		int pageSize = 10;//每页显示10条

		int startIndex = (pageNo - 1) * pageSize;
		string sql = "select ... limit" + startIndex + "," + pageSize;
		}

记公式:
	limit (pageNo-1)*pageSize,pageSize
	
5.关于DQL语句的大总结:
	select
		...
	from
		...
	where
		...
	group by
		...
	having
		...
	order by
		...
	limit
		...
	执行顺序:
		1.from
		2.where
		3.group by
		4.having
		5.select
		6.order by
		7.limit

更多推荐

数据库mysql入门基础--10.limit的用法及通用分页