您的位置:首页 > 数据库 > MySQL

mysql 读取 读取第十行到第十五行数据?该怎么解决

2012-12-25 20:56 615 查看
http://www.myexception.cn/sql-server/137496.html

读取第十行到第十五行数据?

从某个表里面读取第十行到第十五行数据的最快方法是哪一种?

------解决方案--------------------------------------------------------

select top 5 *

from (select top 15 * from 表 order by 主键列 asc)a

order by order by 主键列 desc
------解决方案--------------------------------------------------------

select top 6 a.*

from 表 a

left join

(select top 9 主键 from 表) b

where b.主键 is null

------解决方案--------------------------------------------------------

/*取N到M条记录公式并按ID升序排列*/

Select

*

From (

Select

Top M-N+1 *

From (

Select Top M * From 表

) T

Order By ID Desc

) TT

Order By ID

/*取N到M条记录公式并按ID降序排列*/

Select

Top M-N+1 *

From (

Select Top M * From 表

) T

Order By ID Desc
------解决方案--------------------------------------------------------

----如果没有主键就用临时表

select id=identity(int,1,1),* into # from 表

select * from # where id between 10 and 15
------解决方案--------------------------------------------------------

从某个表里面读取第十行到第十五行数据的最快方法是哪一种?

select top 6 * from tablea where ID not in (select top 9 ID tablea)

--选择n到m条数据

select top m-n+1 * from tablea where ID not in (select top n-1 ID from tablea)

如果是10-15条的话not in 就可以了,只是个例子的话“学习”中~~~
------解决方案--------------------------------------------------------

---测试

Select Top 100 ID=Identity(int,1,1) Into # From sysColumns A,sysColumns B

---取第10到15条数据按ID升序排

Select

*

From (

Select

Top 6 * /*15-10+1=6*/

From (

Select Top 15 * From #

) T

Order By ID Desc

) TT

Order By ID
------解决方案--------------------------------------------------------

如果不是要求灵活的,就直接按照一楼的来就行了
------解决方案--------------------------------------------------------

楼上说的很清除了
------解决方案--------------------------------------------------------

学习 一个很好的例子!谢谢LZ
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: