select top 3 * from a where type = 1 union select top 2  * from a where type = 1可以实现,
但我想满足type=1的随机取3条数据,满足type=1的取随机2条数据怎么实现?
用select top 3 * from a where type = 1 order by newid() union select top 2  * from a where type = 1 order by newid()
就出错,望高手给点指点,谢谢!

解决方案 »

  1.   

    前面不能用: order by newid() 
    select top 3 * from a where type = 1union select top 2  * from a where type = 1 order by newid()
      

  2.   

    union 前不可以用ORDER BY
      

  3.   

    这样也不对啊,报错是说如果语句中包含union运算符,那么order by 子句中的项就必须出现在选择列表中
      

  4.   

    给还有其他的办法实现啊,只要满足type=1的随机取3条数据,满足type=1的取随机2条数据就行
      

  5.   

    select * from (
    select top 3 * from a where type = 1 order by newid()
    ) as t1
    union all
    select * from (
    select top 2  * from a where type = 1 order by newid()
    ) as t2
      

  6.   

    想满足type=1的随机取3条数据,满足type=1的取随机2条数据怎么实现?是不是输入错误想满足type=1的随机取3条数据,满足type=2的取随机2条数据怎么实现?
    select * from (
    select top 3 * from a where type = 1 order by newid()
    ) as t1
    union all
    select * from (
    select top 2  * from a where type = 2 order by newid()
    ) as t2
      

  7.   

    服务器: 消息 104,级别 15,状态 1,行 1
    如果语句中包含 UNION 运算符,那么 ORDER BY 子句中的项就必须出现在选择列表中。
    按照楼上的做法,还是报这样的错误,怎么会事啊,还望楼上的高手指点!
      

  8.   

    select * from (
    select * from (
    select top 3 * from a where type = 1 order by newid()
    ) as t1
    union all
    select * from (
    select top 3 * from a where type = 2 order by newid()
    ) as t2
    )p
      

  9.   

    select * from (
    select * from (
    select top 3 * from a where type = 1 order by newid()
    ) as t1
    union all
    select * from (
    select top 2  * from a where type = 2 order by newid()
    ) as t2
    ) as t
      

  10.   


    select *
    from (select top 3 * from a where type = 1 order by newid())a
    union all
    select * 
    from (select top 2  * from a where type = 1 order by newid())b
    这样为可以了
      

  11.   

    谢谢lxzm1001(*悠悠蓝星*)的解答,已经解决此问题了,同时也谢谢大家的回复!