表1:hot,包括字段(id,tid)
表2:xxb,包括(id,tid,xm,xb,nl,bz)在xxb找出xxb.tid=hot.tid前5条不重复信息,假如不足5条,则把bz=1的凑成5条

解决方案 »

  1.   

    select top  5 id,tid,xm,xb,nl,bz
    from(
    select xxb.*,1 as order_by
    from hot
    join xxb on xxb.tid=hot.tid
    union 
    select top 5 * ,1 as order_by from xxb where bz = 1 order by id
    )x
    order by order_by
      

  2.   

    上面的错了,改改select top  5 id,tid,xm,xb,nl,bz
    from(
    select xxb.*,1 as order_by
    from hot
    join xxb on xxb.tid=hot.tid
    union 
    select top 5 * ,2 as order_by from xxb where bz = 1 order by id
    )x
    order by order_by
      

  3.   

    我需要信息的tid不重复,只取一条tid在hot_top中的,总共5条,不足的就取bz=1