Recently, in the forum, I met a lot more difficult sql problem, although they can be resolved, but found a few days later, they can not remember, forget the solution of.
So, I feel the need to be recorded, so that after the encounter this problem again, and get answers from the idea.
1, a section of SQL wording, seeking method!
http://bbs.csdn.net/topics/390705441
select * from k11 have 10 records
files1 files2 files3 files4
AA BB CC DD
ZZ TT YY EE
NN MM GG UU
another table User 99 records, now random randomly selected three records in the User table inside
select top 3 NewID () AS Random, [the UserId] from [the User]
Random the UserId
E81A4DBB Ming
F31B3B6C Ling
16,574,317 Ting
the User UserId value of the result on the table evenly onto k11 above the table, the final results are shown below
files1 files2 files3 files4 the UserId
A1 BB the CC ming DD
A2 TT EE YY ling
A3 MM GG UU ling
A4 XX XX XX ming
A5 XX XX XX ling
A6 XX XX XX Ting
A7 XX XX XX ming
A8 XX XX XX ling
A9 XX XX XX ling
A10 XX XX XX ming
sql Code:
-
create
table k11(
-
files1
varchar(
10),
-
files2
varchar(
10),
-
files3
varchar(
10),
-
files4
varchar(
10),
-
UserId
varchar(
10))
-
-
insert
into k11
-
select
'A1',
'BB',
'CC',
'DD',
'ming'
union all
-
select
'A2',
'EE',
'TT',
'YY',
'ling'
union all
-
select
'A3',
'MM',
'GG',
'UU',
'Ting'
union all
-
select
'A4',
'XX',
'XX',
'XX',
'ming'
union all
-
select
'A5',
'XX',
'XX',
'XX',
'ling'
union all
-
select
'A6',
'XX',
'XX',
'XX',
'Ting'
union all
-
select
'A7',
'XX',
'XX',
'XX',
'ming'
union all
-
select
'A8',
'XX',
'XX',
'XX',
'ling'
union all
-
select
'A9',
'XX',
'XX',
'XX',
'Ting'
union all
-
select
'A10',
'XX',
'XX',
'XX',
'ming'
-
-
-
create
table [
user]
-
(
-
UserId
varchar(
10)
-
)
-
-
insert
into [
user]
-
select
'ming'
union all
-
select
'ling'
union all
-
select
'Ting'
-
go
-
-
-
;with t
-
as
-
(
-
select [UserId],
-
ROW_NUMBER()
over(
order
by newid())
as
rownum
-
from [
User]
-
),
-
-
tt
-
as
-
(
-
select *,
-
case
when rownum1 %
3 =
0
then
3
else rownum1 %
3
end
as
rownum
-
from
-
(
-
select *,
-
ROW_NUMBER()
over(
order
by
getdate())
as rownum1
-
from k11
-
)a
-
)
-
-
select tt.files1,tt.files2,tt.files3,tt.files4,t.UserId
-
from tt
-
left
join t
-
on tt.rownum = t.rownum
-
and t.rownum <=
3
-
/*
-
files1 files2 files3 files4 UserId
-
A1 BB CC DD ming
-
A2 EE TT YY ling
-
A3 MM GG UU Ting
-
A4 XX XX XX ming
-
A5 XX XX XX ling
-
A6 XX XX XX Ting
-
A7 XX XX XX ming
-
A8 XX XX XX ling
-
A9 XX XX XX Ting
-
A10 XX XX XX ming
-
*/
2, the random data return 100:
If the table is relatively large, there are tens of millions of data, by code, you can return 100 random data, random here, not in the sense of truly random, but it does make people feel is random, that is, each time you return data are not the same, the key is very fast, close to 0 sec consumed only 62 milliseconds.
-
select *
-
from
-
(
-
select *,
-
ROW_NUMBER()
over(
order
by @@servername)
as
rownum
-
from dbo.xxx
-
)t
-
where
rownum
between
CHECKSUM(
getdate()) %
10000
and
CHECKSUM(
getdate()) %
10000+
99
SQL Server 分析和编译时间:
CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。
(100 行受影响)
SQL Server 执行时间:
CPU 时间 = 0 毫秒,占用时间 = 62 毫秒。