declare @t table (id int,test int)
insert into @t select 1,1
insert into @t select 3,3
insert into @t select 4,3
insert into @t select 2,2
insert into @t select 5,3
insert into @t select 6,3
insert into @t select 7,3
select * from @t a where id=(select top 1 id from @t where test=a.test and test =3 order by newid())
大家先猜猜答案,在去调试

解决方案 »

  1.   

    test=a.test and test =3 ,满足条件的就一个呀
    3,3
      

  2.   

    肯定不是,有order by newid(),我猜的是3,4,5,6,7,但是错了,下面的接着猜>>>>>
      

  3.   

    id=(select top 1 id from @t where test=a.test and test =3 order by newid())
    默认升序排列
    test=3的id有3,4,5,6,7
    最小的3
    然后搜索id=3 的就一条呀
      

  4.   

    order by newid()好像是随机的吧
      

  5.   

    不使用临时表create table t(id int,test int)
    insert into t select 1,1
    insert into t select 3,3
    insert into t select 4,3
    insert into t select 2,2
    insert into t select 5,3
    insert into t select 6,3
    insert into t select 7,3
    select * from t a where id=(select top 1 id from t where test=a.test and test =3 order by newid())
      

  6.   

    --会出现重复..
    可用游标写入临时表。。或确定唯一test列后再连
      

  7.   

    每次结果都可能不同
    有个order by newid()了