有一個表t_customer,欄位code為Varchar(20),name為Varchar(40),Code為主鍵欄位,寫一條SQL語句刪除name相同但code不同且code不是相同name對應的最大值的記錄。(限一條SQL語句)

解决方案 »

  1.   


    delete a from table1 a where Code not in(select max(Code) from table1 where Name=a.name)
      

  2.   

    delete t from 表 t where exists(select 1 from 表 where name=t.name and Code>t.Code)
      

  3.   

    其它方法參照http://topic.csdn.net/u/20080626/00/43d0d10c-28f1-418d-a05b-663880da278a.html
      

  4.   

    delete a from t_customer a where  exists(select 1 from t_customer where Name=a.Name and code>a.code)
      

  5.   

    delete t_customer
    where exists (select 1 from t_customer where name=t_customer.name and code<t_customer.code)
      

  6.   

    delete a from t_customer a where code not in (select max(code) from t_customer where Name=a.Name)
      

  7.   

    [code=SQL] delete t_customer aa where  not exists(select 1 from t_customer where name = aa.name and code > aa.code ) [/SQL]