更改密钥后如何修复数据

时间:2009-06-25 10:42:26

标签: mysql

我有一个包含多个整数作为主键的表。其中一个是柜台。我需要从钥匙上取下计数器。

从密钥中删除计数器会使许多行重复(具有不同的计数器值,但所有其他关键元素都相同)。

我需要一个删除所有重复项的查询,只留下具有最​​高计数器值的行。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

这个怎么样:

create table foo (
  a number,
  b number,
  c number,
  constraint pk_foo primary key (a, b, c)
);
insert into foo (a, b, c) values (0, 0, 0);
insert into foo (a, b, c) values (0, 0, 1);
insert into foo (a, b, c) values (0, 1, 0);
insert into foo (a, b, c) values (0, 1, 1);
insert into foo (a, b, c) values (1, 0, 0);
insert into foo (a, b, c) values (1, 0, 1);
insert into foo (a, b, c) values (1, 1, 0);
insert into foo (a, b, c) values (1, 1, 1);
delete from foo t1
 where t1.c not in (
         select max(t2.c)
           from foo t2
          group by a, b
       )
;
select * from foo;

PS:您必须先删除 从主键中删除 c