

Like a good immune system, the bad rows shouldn't even be allowed in to the table at the time of insert. Now define a new composite unique key on your table (on those two columns) to prevent more duplicates from being added in the first place. If you got this to work, and it put out your "duplicate row" fire. Prevent this problem from ever happening again on this table: Make a backup of the table before you run this. Filter down on those NULL rows and you have a set of all rows grouped by foo and bar that isn't the latest timestamp baz.

The right hand subset 'b' has a (maxtimestamp,foo,bar) which is matched to left only on the one that IS the max.Įvery row that is not that max has value maxtimestamp of NULL. The reason we use ‘ purge ‘ instead of ‘ remove ‘ is that the. As you can see, it has removed 3 packages that contain files for the server. (foo,bar,baz) on left has every row in the table. The Ubuntu packages for MySQL Server start with ‘ mysql-server ’ and you can use ‘ apt purge ‘ to remove all these packages. The right hand table 'b' which is a subset finds the max timestamp grouped by columns foo and bar. Table penguins with alias 'a' is left joined on a subset of table penguins called alias 'b'. What's that big SQL delete statement doing? Observe and cleanup drop table penguins_copy #drop the first table and put the copy table back:Ĭreate table penguins select * from penguins_copy #drop the extra column on the copied table The max aggregate operates upon the new moo index: delete a from penguins_copy a left join( drop table if exists penguins_copy Ĭreate table penguins_copy as ( SELECT foo, bar FROM penguins ) ĪLTER TABLE penguins_copy ADD moo int AUTO_INCREMENT PRIMARY KEY first Make a clone of the first table and copy into it. Insert into penguins values(3, 'kowalski') Insert into penguins values(1, 'skipper') You'll have to do additional steps to delete duplicate rows.Ĭreate the penguins table and add some rows create table penguins(foo int, bar varchar(15)) You don't have a timestamp or a unique index column to sort by? You're living in a state of degeneracy. Typically, the AFTER DELETE Trigger is fired promptly after a MySQL Delete query event accomplishes in a table. For those of you without a timestamp or unique column. This MySQL AFTER DELETE Trigger is formed on a database table to reserve summary table connected to it. You're done, duplicate rows are removed, last one by timestamp is kept. Insert into penguins values(4, 'rico', now()) Insert into penguins values(3, 'kowalski', now()) Insert into penguins values(1, 'skipper', now()) Deleting duplicate rows in MySQL in-place, (Assuming you have a timestamp col to sort by) walkthrough:Ĭreate the table and insert some rows: create table penguins(foo int, bar varchar(15), baz datetime)
