sql >> Databasteknik >  >> RDS >> Mysql

Fixa DB-dubblettposter (MySQL-bugg)

För att lista alla avvikelser:

SELECT name, count(*) FROM TableA GROUP BY name HAVING count(*) > 1;

Det finns några sätt att ta bort dups och din väg kommer att bero mycket på antalet dups du har.

Se detta SÅ fråga om sätt att ta bort dem från ditt bord.

Här är lösningen jag gav där:

-- 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. Frågar du en sträng från int-kolumnen?

  2. Använda Oracle JDeveloper 12c med Oracle Database 12c på Oracle Cloud Platform, del 3

  3. PID-fel vid start av mysql.server?

  4. Varning:mysqli_connect():Okänd MySQL-servervärd