select distinct top 4 [b2c_z].to_user_un,[user].user_logo,[b2c_z].id from [b2c_z],[user] where [b2c_z].to_user_un=[user].user_un and [user].user_sex='女' and [user].user_logo_yn='1' order by [b2c_z].id desc加了ORDER BY的话  就有重复值了请问大家怎么办(在不用临时表的前提下)谢谢指教

解决方案 »

  1.   

    Try:select top 4 
    from ( 
    select distinct
    [b2c_z].to_user_un,[user].user_logo,
    [b2c_z].id 
    from [b2c_z],[user] 
    where [b2c_z].to_user_un=[user].user_un 
    and [user].user_sex='女' 
    and [user].user_logo_yn='1' 
    ) as t
    order by id desc
      

  2.   

    谢谢楼上的   但是有错误
    服务器: 消息 156,级别 15,状态 1,行 1
    在关键字 'from' 附近有语法错误。
    服务器: 消息 156,级别 15,状态 1,行 1
    在关键字 'as' 附近有语法错误。
      

  3.   

    看起来没问题,你想取什么数据?加了order by 后先按 [b2c_z].id 排序 ,然后取出 前4 行,前提是记录不重复的情况下排序的,[b2c_z].id 这一列可能会有重复,不会 三列都重复吧
      

  4.   

    少写了*select top 4 *
    from ( 
    select distinct
    [b2c_z].to_user_un,[user].user_logo,
    [b2c_z].id 
    from [b2c_z],[user] 
    where [b2c_z].to_user_un=[user].user_un 
    and [user].user_sex='女' 
    and [user].user_logo_yn='1' 
    ) as t
    order by id desc
      

  5.   

    回楼上    ID是自动编号   我是想按最新的来排序  加了ORDER BY ID   重复值就过滤不掉了
      

  6.   

    扬帆破浪  你好,用了你的SQL 查询出来还是有重复值
      

  7.   

    ...
    看来你要说清楚你希望的不重复是指[b2c_z].to_user_un,[user].user_logo,[b2c_z].id 三个字段的组合不重复还是哪个单个子字段不重复
      

  8.   

    估计是要求[b2c_z].to_user_un,[user].user_logo组合不重复,可以这样select top 4 [b2c_z].to_user_un,[user].user_logo,max([b2c_z].id) as id
    from [b2c_z],[user] 
    where [b2c_z].to_user_un=[user].user_un and [user].user_sex='女' and [user].user_logo_yn='1' 
    group by [b2c_z].to_user_un,[user].user_logo
    order by max([b2c_z].id)