22,294
社区成员
发帖
与我相关
我的任务
分享declare @tb table (id int,name varchar(20))
insert into @tb select 1,'a'
insert into @tb select 2,'b'
insert into @tb select 3,'c'
insert into @tb select 4,'d'
insert into @tb select 5,'e'
insert into @tb select 6,'f'
select a.name,b.id from
(
select row_number() over(order by name) as orderid,name from(
select top 3 name from @tb order by newid())tp1)a ,
(
select row_number() over(order by id) as orderid,id from(
select top 3 id from @tb order by newid())tp2)b
where a.orderid=b.orderid
---楼主是不是要这种效果
create table #tb1(col1 int)
create table #tb2 (col2 int)
insert #tb1 select 1 union all select 2
insert #tb1 select 2 union all select 3
insert #tb2 select 1 union all select 2
insert #tb2 select 1 union all select 2
alter table #tb1
add idcol int identity(1,1)
alter table #tb2
add idcol int identity(1,1)
select * from #tb1
select * from #tb2
select col1,col2 from #tb1,#tb2 where #tb1.idcol=#tb2.idcol
alter table #tb1 drop column idcol
alter table #tb2 drop column idcol
select distinct * from a,b
假设F字段不重复
select m.f f1 , n.f f2 from
(select f,px = (select count(1) from A where f < t.f) + 1 from A t) m,
(select f,px = (select count(1) from B where f < t.f) + 1 from B t) n
where m.px = n.px
如果字段F有重复,使用临时表.
select f , px = identity(int,1,1) into tmpA from A
select f , px = identity(int,1,1) into tmpB from B
select m.f f1 , n.f f2 from tmpA m , tmpB n where m.px = n.pxdeclare @tb table(sName varchar(32), sNo varchar(16))
insert @tb select 'a', '123'
union all select 'b', '234'
union all select 'c', '345'
union all select 'd', '456'
union all select 'e', '567'
declare @# table(id int identity(1,1), nameORno varchar(32))
insert @# select top 3 sName from @tb order by newid()
insert @# select top 3 sNo from @tb order by newid()
select name=a.nameORno, no=b.nameORno
from @# a join @# b on a.id=b.id-3
/*
name no
-------------------------------- --------------------------------
a 234
b 567
c 456
(3 row(s) affected)
*/