sql >> Databasteknik >  >> RDS >> Mysql

mysql radering av dubbletter av data

Du kan skapa en tabell med en post för var och en av dubbletterna:Ta sedan bort alla dup-poster från persontabellen och infoga sedan dup-posterna igen.

-- Setup for example
create table people (fname varchar(10), lname varchar(10));

insert into people values ('Bob', 'Newhart');
insert into people values ('Bob', 'Newhart');
insert into people values ('Bill', 'Cosby');
insert into people values ('Jim', 'Gaffigan');
insert into people values ('Jim', 'Gaffigan');
insert into people values ('Adam', 'Sandler');

-- Show table with duplicates
select * from people;

-- Create table with one version of each duplicate record
create table dups as 
    select distinct fname, lname, count(*) 
    from people group by fname, lname 
    having count(*) > 1;

-- Delete all matching duplicate records
delete people from people inner join dups 
on people.fname = dups.fname AND 
   people.lname = dups.lname;

-- Insert single record of each dup back into table
insert into people select fname, lname from dups;

-- Show Fixed table
select * from people;


  1. SQLite JSON_INSERT()

  2. mysql kontrollera sammanställning av en tabell

  3. Hur kan jag emulera WHERE 1 i en dynamisk fråga?

  4. filtertid