学生表 
   a1,学生号
   a2,姓名
   a3,
   a4,
   a5,
  
  现在出现了a1相同,其他不同的记录(应该是一个学号对应一个姓名)
要求这些重复记录,只保留一条(随机),其他的删除。
求SQL.    

解决方案 »

  1.   

    delete from table_name where 学生号 not in (select min(学生号) 学生号 from table_name group by 学生号);
      

  2.   

    如果是Oracle,可以使用ROWID来删除
      

  3.   

    delete from 学生表 where rowid in (select a.rowid from 学生表 a, 学生表 b where a.rowid>b.rowid 
    and a.学生号=b.学生号)
      

  4.   

    delete from 学生表 where rowid not in (select min(rowid) from 学生表 group by 学号);
      

  5.   


    --保留姓名最大的.
    delete 学生表 from 学生表 t where a2 not in (select max(a2) from 学生表 where a1 = t.a1)--保留姓名最小的.
    delete 学生表 from 学生表 t where a2 not in (select min(a2) from 学生表 where a1 = t.a1)