我有一个组合SQL语句,虽然我用了distin..但是他好像所有字段都相同才选出一条,现在我想这样只要id号相同就取一条,请问能不能实现

解决方案 »

  1.   

    先show你的表结构和你的写法。
      

  2.   

    可以的
    select distinct id from tablename
      

  3.   

    ===================================================================
    select distinct top 5  a.jid,.......from v_ss a left join ynfo b on a.jid=b.jid left join Bntype c on b.qype=c.qype where b.sus=1
    结果:jid    .......
     12   ....
     12   ....
     23  ....
     34  ...
    我要的结果是如果jid相同的只出来一条.
    ===============================================
    ===========================================
    ================================================
      

  4.   

    select distinct id from xxx
      

  5.   

    select distinct top 5  a.jid,.......from v_ss a left join ynfo b on a.jid=b.jid left join Bntype c on b.qype=c.qype where b.sus=1
    结果:jid    .......
     12   ....
     12   ....
     23  ....
     34  ...
    我要的结果是如果jid相同的只出来一条.12   ....
     12   ....
    这两条你怎么知道你要的是哪条呢?
      

  6.   

    select * from f where id in (select max(id) from f group by jid)
    //id是唯一标识列,jid相当与你的id
      

  7.   

    我上面的就可以,id是自增列,jid就是可能重复的id
      

  8.   

    zhaozhao110() ( ) 信誉:100    Blog 
    正确
      

  9.   

    select distinct CDNUM, min(CD)from test group by CDNUM
      

  10.   

    这个应该可以,我刚试过。
    SELECT  MAX(YOURID) FROM CHT GROUP BY YOURID HAVING COUNT(YOURID) > 1
      

  11.   

    Group by jid不就可以啊,不过在聚合的话你得出的记录就少了
      

  12.   

    数据
    a b
    2 2
    3 3
    3 3select top 3 a,b from dbo.test  group by a, b having count(a)>=1
    结果
    2 2
    3 3
      

  13.   

    select distinct top 5  a.jid from v_ss a left join ynfo b on a.jid=b.jid left join Bntype c on b.qype=c.qype where b.sus=1 group by a.jid having count(a.jid)>1
    这样应该可以,distinct时只能选出比较一个字段,如果有select出2个字段的话,要2个字段同时不同才能选的出来,否则distinct是起不到作用的.