查询一个表 要求:共查询5条记录,
前2条固定,按照价格排序,从高到低,如何这两条价格相同,则随机显示
后3条随机显示,不包含前2条查出来的记录,

解决方案 »

  1.   

    ;with cte as (
    select top 2 * from tab order by 价格 desc
    )
    select * from cte 
    union all
    select top 3 * from tab 
    where id not in (select id from cte)
    order by newid()
      

  2.   

    with cte as (
    select top 2 * from tab order by 价格 desc
    )
    select * from cte 
    union all
    select * from (
    select top 3 * from tab 
    where id not in (select id from cte)
    order by newid()
    ) as t