数据库读取中间几条记录的SQL语句

2017-04-23 22:31 阅读 3,397 次 评论 0 条

从表[tableName]中读取符合查询条件的中间几条记录的SQL语句

数据库读取前几条记录的SQL语句大全
数据库读取随机几条记录的SQL语句
1 Access 采用top
从表中取出第 M 条到第 N 条的记录(如N=M+10)

select top N-M+1 * from [tableName] where (id not in (select top M-1 id from [tableName]))

select top N-M+1 * from [tableName] as a where not exists (select * from (select top M-1 * from [tableName] order by id) b where b.id=a.id ) order by id
注意
上述语句不能取从第1条到第N条的数据(即M=1时失效),因为select top N …… 中N必须从1开始(参考:数据库读取前几条记录的SQL语句大全)
此问题的解决办法:要取第1到N条的记录,需要使用select top N …… 解决。

取数据库第20到第30条中间的十条记录的sql语句

select top 10 * from [tableName] where id not in (select top 20 id from [tableName] order by id)

删除前10行

delete from [tableName] where id in(select top 10 id from [tableName])

删除10-20条

delete from [tableName] where id in(select top 20 id from [tableName]) and id not in(select top 10 id from [tableName])

SQL Server
请参考Access部分

3 MySql 采用limit

limit 子句可以被用于强制 select 语句返回指定的记录数。limit 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1)

检索记录行11-15

select * from [tableName] limit 10,15

检索前10个记录行,相当于MS SQL的 top 10

select * from [tableName] limit 5

版权声明:本文著作权归原作者所有,欢迎分享本文,谢谢支持!
转载请注明:数据库读取中间几条记录的SQL语句 | 雨晨博客

发表评论


表情