数据库有600万条数据,查询出每前100万条的前6条数据。

振乾 2009-08-18 11:42:24
数据库有600万条数据,查询出每前100万条的前6条数据。
请大家帮忙!!
...全文
270 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2009-08-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 haiwer 的回复:]
如果有连续的ID

SQL codeselect*from tbwhere idbetween1and6or idbetween1000001and1000006or idbetween2000001and2000006or idbetween3000001and3000006or idbetween4000001and4000006or idbetween5000001and5000006or idbetween6000001and6000006


[/Quote]
学习
昵称被占用了 2009-08-18
  • 打赏
  • 举报
回复
/%的方法效率都太差,不想写
百年树人 2009-08-18
  • 打赏
  • 举报
回复
如果没有连续的ID就构造一个

select px=identity(int,1,1),* into # from tb order by id

select
id,col2,col3,... (除px外的其他字段)
from
#
where
px%1000000 between 1 and 6

像海爷那样列举效率应该会快很多
--小F-- 2009-08-18
  • 打赏
  • 举报
回复
取n到m行

1.
select top m * from tablename where id not in (select top n id from tablename order by id asc/*|desc*/)

2.
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入到临时表
set rowcount n --只取n条结果
select * from 表变量 order by columnname desc

3.
select top n * from
(select top m * from tablename order by columnname) a
order by columnname desc


4.如果tablename里没有其他identity列,那么:
先生成一个序列,存储在一临时表中.
select identity(int) id0,* into #temp from tablename

取n到m条的语句为:
select * from #temp where id0 > =n and id0 <= m

如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:
exec sp_dboption 你的DB名字,'select into/bulkcopy',true


5.如果表里有identity属性,那么简单:
select * from tablename where identity_col between n and m

6.SQL2005开始.可以使用row_number() over()生成行号
;with cte as
(
select id0=row_number() over(order by id),* from tablename
)
select * from cte where id0 between n to m
feixianxxx 2009-08-18
  • 打赏
  • 举报
回复
select DISTINCT *
from tb t
where id in(select top 6 id from tb where id/1000000=t.id/1000000)
feixianxxx 2009-08-18
  • 打赏
  • 举报
回复
select *
from tb t
where id in(select top 6 id from tb where id/1000000=t.id/1000000)
昵称被占用了 2009-08-18
  • 打赏
  • 举报
回复
如果有连续的ID

select * from tb
where id between 1 and 6
or id between 1000001 and 1000006
or id between 2000001 and 2000006
or id between 3000001 and 3000006
or id between 4000001 and 4000006
or id between 5000001 and 5000006
or id between 6000001 and 6000006


SQL77 2009-08-18
  • 打赏
  • 举报
回复
SELECT * FROM TB T WHERE ID IN (SELECT TOP 6 ID FROM TB WHERE NAME=T.NAME ORDER BY ID ASC)
昵称被占用了 2009-08-18
  • 打赏
  • 举报
回复
怎么感觉需求有点BT

随机36条好不好

select top 36 * from tb order by newid()
水族杰纶 2009-08-18
  • 打赏
  • 举报
回复
每前100W是靠什麼分組?

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧