select top 10 * into #t1 from yourtable where col='条件1' order by newid()
select top 10 * into #t2 from yourtable where col='条件2' order by newid()
select top 10 * into #t3 from yourtable where col='条件3' order by newid()select * from #t1 
union
select * from #t2
union
select * from #t3

解决方案 »

  1.   

    还加上:
    drop table #t1
    drop table #t2
    drop table #t3
      

  2.   


    select * from (
    select * from (
    select top 10 * from tablename
    where 条件A
    order by newid()
    ) as t1
    union 
    select * from (
    select top 10 * from t_data
    where 条件B
    order by newid()
    ) as t2
    union 
    select * from (
    select top 10 * from t_data
    where 条件C
    order by newid()
    ) as t3
    ) as x
      

  3.   

    select * from (
    select * from (
    select top 10 * from tablename
    where 条件A
    order by newid()
    ) as t1
    union 
    select * from (
    select top 10 * from t_data
    where 条件B
    order by newid()
    ) as t2
    union 
    select * from (
    select top 10 * from t_data
    where 条件C
    order by newid()
    ) as t3
    ) as x
    order by newid()            ---------最后次序打乱
      

  4.   

    用union all 不建议用unionselect * from (select top 10 * from 表 where 科目='数学' order by newid()) a
    union all
    select * from (select top 10 * from 表 where 科目='语文' order by newid()) a
    union all
    select * from (select top 10 * from 表 where 科目='英语' order by newid()) a
      

  5.   

    用union all 不建议用unionselect * from (select top 10 * from 表 where 科目='数学' order by newid()) a
    union all
    select * from (select top 10 * from 表 where 科目='语文' order by newid()) a
    union all
    select * from (select top 10 * from 表 where 科目='英语' order by newid()) a
      

  6.   

    select * from (select * from (select top 10 * from 表 where 科目='数学' ) a
    union all
    select * from (select top 10 * from 表 where 科目='语文' ) a
    union all
    select * from (select top 10 * from 表 where 科目='英语' ) a ) b order by newid()
      

  7.   

    select col1,col2,... from (select top 10 *,newid() as IDD from yourtable where 科目='数学') AA
    union all
    select col1,col2,... from (select top 10 *,newid() as IDD from yourtable where 科目='语文') BB
    union all
    select col1,col2,... from (select top 10*,newid() as IDD from yourtable where 科目='英语') CC
      

  8.   

    转贴
    select topic from (select top 20 topic , newid() as id from exams where 科目 = '数学' order by id) t
    union
    select topic from (select top 10 topic , newid() as id from exams where 科目 = '语文' order by id) t2
    union
    select topic from (select top 15 topic , newid() as id from exams where 科目 = '英语' order by id) t3
    or if you are using ADO.NET, you could utilize the NextRecordset method of Recordset object