各种取数据库取前N条记录的Sql语句
2011-05-11 13:26
260 查看
access:
select top (10) * from table1
db2:
select 列名 from table1 where 1=1 fetch first 10 rows only
mysql:
select * from table1 limit 10 或 select * from table1 limit 0,10
sql server:
读取前10条:select top (10) * from table1 where 1=1
在sqlserver里面,若每页显示perpage条记录,按照id的排序,取第pageno页的记录:
select top perpage*pageno * from table where id not in(select top (pageno-1)*perpage id from table1)
oracle取前10条:
select * from table1 where rownum<=10
oracle取第11-20条:
select * from (select *,rownum num from table1 where rownum<=20) where num>=10;
oracle按id排序后取11-20条
select * from (select *,rownum num from (select * from table1 tt order by id desc) where rownum<=20) where num>=10;
关于mysql 的limit 子句的使用方法:
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初 始记录行的偏移量是 0(而不是 1): 为了与 PostgreSQL 兼容,MySQL 也支持句法: LIMIT # OFFSET #。
mysql> SELECT * FROM table LIMIT 5,10; // 检索记录行 6-15
//为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1:
mysql> SELECT * FROM table LIMIT 95,-1; // 检索记录行 96-last.
//如果只给定一个参数,它表示返回最大的记录行数目:
mysql> SELECT * FROM table LIMIT 5; //检索前 5 个记录行
//换句话说,LIMIT n 等价于 LIMIT 0,n。
select top (10) * from table1
db2:
select 列名 from table1 where 1=1 fetch first 10 rows only
mysql:
select * from table1 limit 10 或 select * from table1 limit 0,10
sql server:
读取前10条:select top (10) * from table1 where 1=1
在sqlserver里面,若每页显示perpage条记录,按照id的排序,取第pageno页的记录:
select top perpage*pageno * from table where id not in(select top (pageno-1)*perpage id from table1)
oracle取前10条:
select * from table1 where rownum<=10
oracle取第11-20条:
select * from (select *,rownum num from table1 where rownum<=20) where num>=10;
oracle按id排序后取11-20条
select * from (select *,rownum num from (select * from table1 tt order by id desc) where rownum<=20) where num>=10;
关于mysql 的limit 子句的使用方法:
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初 始记录行的偏移量是 0(而不是 1): 为了与 PostgreSQL 兼容,MySQL 也支持句法: LIMIT # OFFSET #。
mysql> SELECT * FROM table LIMIT 5,10; // 检索记录行 6-15
//为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1:
mysql> SELECT * FROM table LIMIT 95,-1; // 检索记录行 96-last.
//如果只给定一个参数,它表示返回最大的记录行数目:
mysql> SELECT * FROM table LIMIT 5; //检索前 5 个记录行
//换句话说,LIMIT n 等价于 LIMIT 0,n。
相关文章推荐
- SQL语句——选择数据库中第n个记录到第m个记录之间的数据
- ----------删除数据库中重复记录的SQL语句(转)--------------------
- Oracle 修改数据库库表的各种情况和SQL语句
- NDatabase 入门,简单使用 增删改查。让NDatabase带你脱离ADO.net,各种SQL 语句,各种DBMS,各种CRM,IOC之类的烦恼。我们也不需要仓库设计模式了,你妹的。不要表了,不要设计数据库字段了。就这样!
- 各种数据库分页查询sql语句
- SQL语句查询数据库中重复记录的个数
- 用一条SQL语句实现向数据库中插入多条记录的方法
- SQL语句查询表中的第一条记录在不同数据库中的不同之处
- 用SQL语句查询数据库中某一字段下相同值的记录方法
- T-SQL语句实现清空数据库数据的代码,保留原有表形式和各种约束
- T-SQL语句实现清空数据库数据的代码,保留原有表形式和各种约束
- SQL在各种数据库中获取随机数据记录
- SQL语句查询数据库表占用和记录数
- 各种数据库分页查询sql语句大全
- 跨服务器,跨数据库,多表联合查询 / 如何用sql语句来查询表中哪些记录是重复的
- 使用SQL语句获取数据库中随机N个记录
- 数据库sql语句笔试题--包含创建数据库、表、插入记录、查询等操作
- 用SQL语句查询数据库中某一字段下相同值的记录方法
- 在不同SQL服务器上的数据库之间表记录的复制(sql语句实现)
- 数据库查询前N条记录sql语句介绍