Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

penghapusan data duplikat mysql

Anda dapat membuat tabel dengan 1 record untuk setiap duplikat:Kemudian hapus semua record dup dari tabel people, lalu masukkan kembali record dup.

-- 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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Impor file .sql di Access

  2. Panggil ke hasil fungsi anggota () menggunakan CodeIgniter

  3. Tampilkan item hari ini

  4. Bagaimana cara menggunakan regex di MySQL?

  5. Lancar NHibernate DuplicateMappingException dengan AutoMapping