sql数据有几百万条记录,如何应用查询语句查询出1-10000、10000-20000、以此类推。

2025-03-24 18:30:40
推荐回答(3个)
回答1:

create table t5 ( item varchar(20))

insert t5 select '1-10000'
union all select '10000-20000'
union all select '20000-30000'
union all select '40000-50000'

create table t6 (id int identity(1,1),item varchar(20))
insert into t6
select * from t5

create table #t6 (id int,item varchar(20))

declare @i int
declare @c int

select @i= min(id) from t6
select @c=max(id) from t6

while (@i<@c)
begin
insert into #t6
select t1.id, t1.item from t6 t1,t6 t2 where substring(t2.item, 1, Charindex('-',t2.item)-1)=substring(t1.item, Charindex('-',t1.item)+1,len(t1.item)) and t2.id=@i

insert into #t6
select t1.id, t1.item from t6 t1,t6 t2 where substring(t1.item, 1, Charindex('-',t1.item)-1)=substring(t2.item, Charindex('-',t2.item)+1,len(t2.item)) and t1.id=@i

set @i=@i+1

end

select distinct * from #t6

回答2:

你用的什么数据库,SQL SERVER还是别的?

回答3:

高手