怎么样写SQL语句,求出表a中的完全相同的记录,找出并将其删除只剩余一条(相同的有两条以上的要求删除的只剩一条)。要求只用SQL语句写。

解决方案 »

  1.   

    如果你的表中有个不重复的FID字段,同时有FName(某个名称)的话:
    delete *  from  yourtable
      where not 
         (fid in 
               (select distinct min(fid)  
                 from  yourtable 
                   where fname in (select distinct fname  from  yourtable ) 
                )
          )
      

  2.   

    select distinct * into temptable from info_tbl_agent;
    truncate table info_tbl_agent;
    insert info_tbl_agent select * from temptable;
      

  3.   

    我来给楼上老兄注释:
    1、按所有你不希望重复得字段分组得结果导入临时表:如
    select id,字段1,字段2,....count(*) as num into #temp from table1  
    group by id,字段1,字段2,...
    order by id2、删除原表中所有记录,从临时表取数
    insert table1 (id,字段1,字段2,....)
    select id,字段1,字段2,.... from #temp  
      

  4.   

    select distinct *  into #temp from  a
     drop a 
    insert a select * from #temp
    drop #temp
      

  5.   

    select price,max(convert(int,code))  from bijiao group by price having(count(code)>1)

    select price,count(code) as b from bijiao group by price having(count(code)>1)
    我只设了两个字段,code字段不一定是唯一的
      

  6.   

    给个例子你看
    SELECT ID, PNAME, PRICE, NUM, PDESCRIPTION 
    INTO newtable3
    FROM newtable
    GROUP BY ID, PNAME, PRICE, NUM, PDESCRIPTION
    HAVING COUNT(*) > 1select * from newtable3DELETE FROM newtable
    WHERE ID IN (
    SELECT ID
    FROM newtable
    GROUP BY ID, PNAME, PRICE, NUM, PDESCRIPTION
    HAVING COUNT(*) > 1
    )select * from newtableINSERT  newtable
          ( id,pname, price, num, pdescription)
    select newtable3.id, newtable3.pname,newtable3.price,
        
    newtable3.num, newtable3.pdescription
    from newtable3