Domanda

Questo mi mostra tutti i nomi e gli ultimi nomi che hanno esattamente due voci che sono identici

SELECT `firstname`,`lastname`,COUNT(*) AS Count 
FROM `people` 
GROUP BY `firstname`,`lastname`
HAVING Count = 2

Come faccio a trasformare questo in un DELETE FROM WHERE con un limite per rimuovere solo uno di ognuna delle voci e lasciare l'altro.

va bene questo sembra essere il modo per tecnica Sto solo andando a farlo in un ciclo while php

È stato utile?

Soluzione

È possibile creare una tabella con 1 registro di ciascuno dei duplicati:. Quindi eliminare tutti i record duplicati dalla tabella persone e poi re-inserire i record duplicati

-- 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;

Altri suggerimenti

se si dispone di una chiave primaria, quali ID, si può fare:

delete from people 
where id not in
(
      select minid from 
      (select min(id) as minid from people 
      group by firstname, lastname) as newtable
)

Il bit sottoquery select min(id)... si sta ottenendo le righe univoche (sulla base di id) per un dato cognome, cognome combinazione; e quindi si sta cancellando tutte le altre righe, vale a dire i duplicati. È necessario per avvolgere la subquery causa di un bug in MySQL, altrimenti potremmo fare:

delete from people 
where id not in
(
      select min(id) as minid from people 
      group by firstname, lastname
)

meglio sarebbe:

delete people from 
people left outer join
(
  select min(id) as minid from people 
  group by firstname, lastname
) people_grouped
on people.first_name = people_grouped.first_name
and people.last_name = people_grouped.last_name
and people_grouped.id is null

per evitare il subquery.

Creare una nuova tabella e aggiungere una chiave univoca su (nome, cognome). Quindi inserire le righe nella vecchia tabella nella nuova tabella. Quindi rinominare le tabelle.

mysql> select * from t;
+-----------+----------+
| firstname | lastname |
+-----------+----------+
| A         | B        | 
| A         | B        | 
| X         | Y        | 
+-----------+----------+
3 rows in set (0.00 sec)

mysql> create table t2 like t;
Query OK, 0 rows affected (0.00 sec)

mysql> alter table t2 add unique key name(firstname,lastname);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> insert ignore into t2 select * from t;
Query OK, 2 rows affected (0.00 sec)
Records: 3  Duplicates: 1  Warnings: 0


mysql> select * from t2;
+-----------+----------+
| firstname | lastname |
+-----------+----------+
| A         | B        | 
| X         | Y        | 
+-----------+----------+
2 rows in set (0.01 sec)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top