日期:2014-05-18  浏览次数:20345 次

随机查询数据库数据的问题!请各位大侠帮忙啊!
我有一张表mytable!比如里面只有3个字段,
mobile,province,city   都是char型
我现在要想随机查询表里的内容!

(mobile为手机号-前7个字符为手机的号段(比如13656565656   1365656为号段),共有80个号段,province为省份-已知有31个省)
条件:随机抽40个号段,10个省份,每个号段750个记录   共300000个记录!

请高手指点一下了!谢谢!

------解决方案--------------------
declare @p varchar(20),@s varchar(10)
declare @t table(mobile,province,city)
declare p_cursor cursor
for
select top 10 province from mytable order by newid()
open p_cursor
fetch next from p_cursor into @p
while(@@fetch_status=0)
begin
declare s_cursor cursor for select top 40 s from
(select substring(mobile,1,7) s from mytable group by substring(mobile,1,7)) a
order by newid()
open s_cursor
fetch next from s_cursor into @s
while (@@fetch_status=0)
begin
insert into @t select top 750 mobile,province,city from mytable where substring(mobile,1,7)=@s
fetch next from s_cursor into @s
end
close s_cursor
deallocate s_cursor
fetch next from p_cursor into @p
end
close p_cursor
deallocate p_curosr

--没有测试过
------解决方案--------------------
insert into @t select top 750 mobile,province,city from mytable where substring(mobile,1,7)=@s
-->
insert into @t select top 750 mobile,province,city from mytable where substring(mobile,1,7)=@s order by newid()