表t,栏位code为varchar(20),name为varchar(40),code为主键栏位,写一条sql语句删除name相同code不同且code 不是相同name对应的最大值得记录。

解决方案 »

  1.   

    delete t a
    where not exists(select 1 from t where a.name=name and code<a.code )
      

  2.   

    ----保留name相同的行中code最大的行
    delete a from table as a where 
    exists( select 1 from table where name = a.name and code > a.code)
      

  3.   

    delete t a
    where not exists(select 1 from t where a.name=name and code<a.code )
      

  4.   

    delete #t 
    where exists(select 1 from #t b where b.name=#t.name and b.code>#t.code)
      

  5.   

    code 如果是12345,这样的,但是以 varchar存储的.那么比较code最大值时最好用
    convert转换成数值再比较,否则可能出错.
    比较字符串时,'21111'比'31'小,实际楼主的意思可能是按照其对应的数值比较.