表useronline 有20条记录
ID,userid,UserName,moduleID,CurrentPlace,time表quan_board有100 条记录
ID,moduleID,CurrentPlace
从表quan_board中随机取出字段ModuleID,CurrentPlace的20条记录
更新表useronline中的ModuleID和CurrentPlace字段
请教这个怎么弄

解决方案 »

  1.   

    update t
    set t.ModuleID = a.ModuleID,
        t.CurrentPlace =a.CurrentPlace
    from useronline t,(select top 20 * from quan_Board  ) as a
    where t.ID = a.ID
      

  2.   

    declare @t table(ID int,userid int,UserName varchar(10),moduleID int,CurrentPlace varchar(10),time varchar(10))
    declare @a table(ID int,moduleID int,CurrentPlace varchar(10))insert into @t select 1,1,'qq',1331,'ee','ee'
    insert into @t select 2,2,'aa',13,'e','3g'
    insert into @t select 4,11,'rr',135,'g','g'
    insert into @t select 6,13,'gg',14,'gt','y'
    insert into @t select 7,14,'ee',13,'bh','r'insert into @a select 1,null,null
    insert into @a select 2,null,null
    insert into @a select 4,null,null
    insert into @a select 8,null,null
    insert into @a select 7,null,nullupdate a set a.moduleID=b.moduleID,a.CurrentPlace=b.CurrentPlace from @a a,(select top 2 ID,moduleID,CurrentPlace from @t order by newid())b where a.id=b.id
    select * from @a--这样吗?由于数据数量问题,top后面我写的2,测试时改为20即可
      

  3.   

    楼上两位:既然是随机取出来的,为什么可以用a.id=b.id这样做关联那?