查询数据库中满足条件的特定行数据
2007-06-20 16:24
387 查看
查询数据库中满足条件的特定行数据,在这里主要给出三条查询语句,其中第三条主要是针对SQL Server2005数据库的,因为其中的Row_Number()函数只有在SQL Server2005中才支持。
例子:
我数据库中有一个table表,表中一共有50条数据,我现在要查询第21到30条数据,我可以对这50条数据分成5页,每页10条数据。
一、select top 页大小 * from table1 where (id not in (select top (页大小-1)*每页数 id from 表 order by id))order by id
例子:select top 10 * from table where (id not in (select top 20 id from table order by id))order by id
二、select top 页大小 * from table1 where id>(select max (id) from (select top ((页码-1)*页大小) id from table1 order by id) as t) order by id
例子:select top 10 * from table where id>(select max (id) from (select top 20 id from table order by id) as t) order by id
总结:二比一好,not in费时
三、select * from(select ROW_NUMBER() over(order by id) -1 as rownum,table * from
依据什么排序 默认行号为-1+1=0 table) as d where rownum between 0 and 10 起始行 显示多少行
例子:select * from(select ROW_NUMBER() over(order by ID desc) as rownum,table * from table) as d where rownum between 21 and 30
例子:
我数据库中有一个table表,表中一共有50条数据,我现在要查询第21到30条数据,我可以对这50条数据分成5页,每页10条数据。
一、select top 页大小 * from table1 where (id not in (select top (页大小-1)*每页数 id from 表 order by id))order by id
例子:select top 10 * from table where (id not in (select top 20 id from table order by id))order by id
二、select top 页大小 * from table1 where id>(select max (id) from (select top ((页码-1)*页大小) id from table1 order by id) as t) order by id
例子:select top 10 * from table where id>(select max (id) from (select top 20 id from table order by id) as t) order by id
总结:二比一好,not in费时
三、select * from(select ROW_NUMBER() over(order by id) -1 as rownum,table * from
依据什么排序 默认行号为-1+1=0 table) as d where rownum between 0 and 10 起始行 显示多少行
例子:select * from(select ROW_NUMBER() over(order by ID desc) as rownum,table * from table) as d where rownum between 21 and 30
相关文章推荐
- 查询数据库中满足条件的特定行数据
- postgresql数据库使用函数查询所有符合条件的表名,以及删除所有查询到的表的数据
- 改进版,全数据库查询含有特定数据的表名字段,简单的冒泡写法
- 表单POST请求服务器,通过多条件查询获取数据库数据
- Oracle查询数据库中某用户下满足某些条件的表的记录数
- 查询分组数据指行条件满足的行的上一条记录的技巧
- MySQL 设置数据按条件查询下的序号 并存入数据库
- PHP查询数据库中满足条件的记录条数(二种实现方法)
- 在数据库中查询特定日期数据的方法
- 加载数据库表全部数据到GridView控件,但通过查询条件在GridView显示一行或几行数据
- PHP查询数据库中满足条件的记录条数(二种实现方法)
- PHP查询数据库中满足条件的记录条数(两种实现方法)
- Hibernate 查询用【in】条件去查询数据库的数据
- PHP连接数据库&通过下拉列表及具体条件查询数据
- mybatis多条件查询满足要求的最多N条最新数据
- 取得数据库中数据 查询条件where使用规则
- sql 一个6亿的表a,一个3亿的表b,通过外间tid关联,你如何最快的查询出满足条件的第50000到第50200中的这200条数据记录。
- MySql条件查询不到数据 → 更改数据库编码
- 实现多条件查询 匹配数据库字段中多个数据
- PHP查询数据库中满足条件的记录数