有一个表t_customer,列code 为varchar(20),name为varchar(40);code为主键列且不同,写一条sql语句删除name相同且想同name中codec 对应值最大的记录(仅一条SQL语句)。

解决方案 »

  1.   

    delete from t_customer t 
    where exists (select 1 from t_customer t1 where t1.name = t.name and t.code > t1.code);
      

  2.   

    删除重复的记录中code最大的一条记录,而不是留下最大的一条记录删掉其他?
    delete from t_customer t 
    where not exists (select 1 from t_customer t1 where t1.name = t.name and t.code < t1.code);
      

  3.   

    delete  from  t_customer where name in (
    select max(code )  from t_customer group by name
    )
      

  4.   

    更正下:delete  from  t_customer where code in ( 
    select max(code )  from t_customer group by name 
    )
      

  5.   

    delete from t_customer t
    where exists (select 1 from t_customer t1 where t1.name = t.name and t.code < t1.code);删除最大的记录 应该是这样 呵呵
      

  6.   

    SQL> select * from t1;        ID NAME
    ---------- --------------------
             1 wh
             2 wh
             3 wh
             4 wp
             5 wr
             6 wq
             7 wy7 rows selected.SQL> delete from t1
      2  where not exists(select 1 from t1 t where t.name=t1.name and t1.id<t.id);5 rows deleted.SQL> select * from t1;        ID NAME
    ---------- --------------------
             1 wh
             2 whSQL>
    我试了下你这个 
    好像不太对哦?
      

  7.   

    恩,#1也是错的SQL> delete from t t1
      2  where (t1.id, t1.name) in (select max(id),name from t t2 group by name having count(1) >= 2) ;1 row deletedSQL> select * from t;        ID NAME
    ---------- --------------------
             1 wh
             2 wh
             7 wy
             6 wq
             5 wx
             4 wr6 rows selected
      

  8.   

    delete from table a where code =(select max( code) from  table b where a.name=b.name);
      

  9.   

    --只保留name相同code最小的一条记录
    delete from t_customer a
    where exists (select code,name
                    from (select name,min(code) code
                             from t_customer 
                         group by name
                          ) b
                   where a.name = b.name
                     and a.code <> b.code);
      

  10.   

    delete from t_customer b
    where code in  (select max(code) code
                            from t_customer a where a.name1=b.name1)delete from t_customer b
     where code in (select max(code) over(partition by name1 order by name1) code
                               from t_customer)
      

  11.   

    呵呵 恩 在7#我也理解错了 
    如果按照1#的那种写法不管用大于号还是小于号都不对
    SQL> delete from t1
      2  where exists(select 1 from t1 t where t.name=t1.name and t.id>t1.id);2 rows deleted.SQL> select * from t1;        ID NAME
    ---------- --------------------
             3 wh
             4 wp
             5 wr
             6 wq
             7 wySQL> rollback;Rollback complete.SQL> delete from t1
      2  where exists(select 1 from t1 t where t.name=t1.name and t.id<t1.id);2 rows deleted.SQL> select * from t1;        ID NAME
    ---------- --------------------
             1 wh
             4 wp
             5 wr
             6 wq
             7 wy
    SQL> select * from t1;        ID NAME
    ---------- --------------------
             1 wh
             2 wh
             3 wh
             4 wp
             5 wr
             6 wq
             7 wy7 rows selected.
    SQL> delete from t1
      2  where (t1.id,t1.name) in (select max(t.id),t.name from t1 t group by t.name
     having count(1)>=2);1 row deleted.SQL> select * from t1;        ID NAME
    ---------- --------------------
             1 wh
             2 wh
             4 wp
             5 wr
             6 wq
             7 wy6 rows selected.
      

  12.   

    你这两种写法都不太对哦 呵呵 原因和我在9#说的一样
    SQL> select * from t1;        ID NAME
    ---------- --------------------
             1 wh
             2 wh
             3 wh
             4 wp
             5 wr
             6 wq
             7 wy7 rows selected.SQL> delete from t1
      2  where t1.id in
      3  (select max(id) from t1 t where t.name=t1.name);5 rows deleted.SQL> select * from t1;        ID NAME
    ---------- --------------------
             1 wh
             2 whSQL> rollback;Rollback complete.SQL> select * from t1;        ID NAME
    ---------- --------------------
             1 wh
             2 wh
             3 wh
             4 wp
             5 wr
             6 wq
             7 wy7 rows selected.SQL> delete from t1
      2  where t1.id in
      3  (select max(id) over(partition by name order by name) id from t1);5 rows deleted.SQL> select * from t1;        ID NAME
    ---------- --------------------
             1 wh
             2 whSQL>