Oracle, DB2 及 MySQL 分页查询写法
现在在WEB 应用中使用分页技术越来越普遍了,其中利用数据库查询分页是一种效率比较高的方法,
下面列出了Oracle, DB2 及 MySQL 分页查询写法。
一:Oracle
select * from (select rownum,name from table where rownum <=endIndex )
where rownum > startIndex
二:DB2
DB2分页查询
SELECT * from (Select 字段1,字段2,字段3,rownumber() over(ORDER BY 排序用的列名 ASC) AS rn from 表名) AS a1 WHERE a1.rn BETWEEN 10 AND 20
以上表示提取第10到20的纪录
select * from (select rownumber() over(order by id asc ) as rowid from table where rowid <=endIndex ) AS a1
where a1.rowid > startIndex
三:MySQL
select * from table limit start,pageNum
SELECT EVENTID , EVENTNAME , CREATETIME , ELAPSEDTIME , REPEATCOUNT , MESSAGE , LOCALINSTANCEID , STATUS , MEMO from(SELECT rownumber() OVER (ORDER BY CREATETIME ASC) AS rowid from MS_EVENT WHERE rowid <=20) as a1 WHERE a1.rowid >10
SELECT * from (SELECT EVENTID , EVENTNAME , CREATETIME , ELAPSEDTIME , REPEATCOUNT , MESSAGE , LOCALINSTANCEID , STATUS , MEMO,
ROWNUMBER() OVER (ORDER BY CREATETIME ASC) AS rn from MS_EVENT) AS a1 WHERE a1.rn BETWEEN 10 and 20
--------------------------------------------------------------------
1. 标准的rownum分页查询使用方法:
select *
from (select c.*, rownum rn from content c)
where rn >= 1
and rn <= 5
2. 但是如果, 加上order by addtime 排序则数据显示不正确
select *
from (select c.*, rownum rn from content c order by addtime)
where rn >= 1
and rn <= 5
解决方法,再加一层查询,则可以解决,
select *
from (s
相关文档:
过程中的事务
定义过程p1
create or replace procedure p1
as
begin
insert into student values(5,'xdh','m',sysdate);
rollback;
end;
定义过程p2
create or replace procedure p2
as
begin
update student set stu_sex = 'a' where stu_id = 3;
p1;
end;
执行过程p2
exec p2;
执行完毕发现 ......
1) 用SELECT语句从表中提取查询数据。语法为
SELECT [DISTINCT] {column1,column2,…} from tablename WHERE {conditions} GROUP BY {conditions} ORDER BY {expressions} [ASC/DESC];
说明:SELECT子句用于指定检索数据库的中哪些列,from子句用于指定从哪一个表或视图中检索数据。
2) ......
一 数据库的事务处理
定义:事务是一组相关的数据改变的逻辑集合。在一个事务中的数据改变(DML)保持着一致的状态,数据的改变同时成功或者同时失败。
二 数据库的事务由下列语句组成
一组DML语句,修改的数据在他们中保持一致
一个 DDL (Data Define Language) 语句
一个 DCL (Data Control Language)语句
1、开 ......
实例一:无参的存储过程
$conn = mysql_connect('localhost','root','root') or die ("数据连接错误!!!");
mysql_select_db('test',$conn);
$sql = "
create procedure myproce()
begin
INSERT INTO user (id, username, sex) VALUES (NULL, 's', '0');
end;
";
mysql_query($sql);//创建一个myproce的存储过程
......