MYSQL的简单查询 Limit可以实现分页
SELECT * FROM `e-commerce`.computer c where c.price=15 Having c.id in (1,2) order by c.id desc limit 0,1 ;
SELECT u.username FROM `user` u join contact_info c on (u.id=c.user_id) and c.address='123';
update customer set username='zhouxiaoyu' where id = 1;
delete from customer where id=1;
insert into customer value(1,'zxy','123456',21,'645144985@qq.com');
SELECT * FROM customer c;
SELECT * FROM `e-commerce`.computer c where c.price=15 group by c.price Having c.id in (1,2);
SELECT * FROM `e-commerce`.computer c where c.price=15 Having c.id in (1,2) order by c.id desc;
Oracle分页查询格式:
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM TABLE_NAME) A
WHERE ROWNUM <= 40
)
WHERE RN >= 21
其中最内层的查询SELECT * FROM TABLE_NAME表示不进行翻页的原始查询语句。ROWNUM <= 40和RN >= 21控制分页查询的每页的范围。
上面给出的这个分页查询语句,在大多数情况拥有较高的效率。分页的目的就是控制输出结果集大小,将结果尽快的返回。在上面的分页查询语句中,这种考虑主要体现在WHERE ROWNUM <= 40这句上。
选择第21到40条记录存在两种方法,一种是上面例子中展示的在查询的第二层通过ROWNUM <= 40来控制最大值,在查询的最外层控制最小值。而另一种方式是去掉查询第二层的WHERE ROWNUM <= 40语句,在查询的最外层控制分页的最小值和最大值。这是,查询语句如下:
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM TABLE_NAME) A
)
WHERE RN BETWEEN 21 AND 40
对比这两种写法,绝大多数的情况下,第一个查询的效率比第二个高得多。
SQL 分页查询格式:
1、使用Top和Not IN:
select Top 页大小* from TestTable where ID Not In(select Top 页大小*(页数-1) ID from TestTable Order By ID) Order By ID
2、利用ID大于多少和Select Top分页:
select Top 页大小* from TestTable where ID>( select max(id) from (select top 页大小*(页数-1) id from testTable order by id) as t) order by ID
3、利用SQL的游标存储过程分页:
create procedure XiaoZhengGe
@sqlstr nvarchar(4000), --查询字符串
@currentpage int, --第N页
@pagesize int --每页行数
as
set nocount on
declare @P1 int, --P1是游标的id
@rowcount int
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
select ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,@currentpage as 当前页
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
set nocount off
通过SQL 查询分析器,显示比较:我的结论是:
分页方案二:(利用ID大于多少和SELECT TOP分页)效率最高,需要拼接SQL语句
分页方案一:(利用Not In和SELECT TOP分页) 效率次之,需要拼接SQL语句
分页方案三:(利用SQL的游标存储过程分页) 效率最差,但是最为通用
--转自