有表tbl
create tbl(id int, rid int, gid int);
希望得到表中id, rid不重复的记录,这个select语句如何写呢?

解决方案 »

  1.   

    select * from tb1 a,tb1 b where a.id<>b.id and a.rid<>b.rid;
      

  2.   

    1楼写错了select id , rid , gid  from tb1 group by id,rid
      

  3.   

    select * From tb1 where strcmp(id,rid) <>0
      

  4.   

    参考下贴中的多种方法http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
    [征集]分组取最大N条记录方法征集,及散分....
      

  5.   

    select * from tbl a 
    where not exists (select 1 from tbl where id=a.id and rid=a.rid and gid >a.gid)
      

  6.   

    如果对结果没有要求则可以直接select id,rid,gid from tbl 
    group by id,rid
      

  7.   

    select * from(select count(*) as 重复数,id,rid from tbl group by id,rid) where 重复数>1
      

  8.   

    不好意思,错了
    select * from(select count(*) as 重复数,id,rid from tbl group by id,rid) where 重复数<=1