Aqui está um processo de exclusão mais simplificado:
CREATE TABLE emailUnique LIKE emailTable;
ALTER TABLE emailUnique ADD UNIQUE INDEX (email);
INSERT IGNORE INTO emailUnique SELECT * FROM emailTable;
SELECT * FROM emailUnique;
ALTER TABLE emailTable RENAME emailTable_old;
ALTER TABLE emailUnique RENAME emailTable;
DROP TABLE emailTable_old;
Aqui estão alguns dados de exemplo:
use test
DROP TABLE IF EXISTS emailTable;
CREATE TABLE `emailTable` (
`id` mediumint(9) NOT NULL auto_increment,
`email` varchar(200) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
INSERT INTO emailTable (email) VALUES
('redwards@gmail.com'),
('redwards@gmail.com'),
('redwards@gmail.com'),
('redwards@gmail.com'),
('rolandoedwards@gmail.com'),
('rolandoedwards@gmail.com'),
('rolandoedwards@gmail.com'),
('red@gmail.com'),
('red@gmail.com'),
('red@gmail.com'),
('rolandoedwards@gmail.com'),
('rolandoedwards@gmail.com'),
('rolandoedwards@comcast.net'),
('rolandoedwards@comcast.net'),
('rolandoedwards@comcast.net');
SELECT * FROM emailTable;
Eu os corri. Aqui estão os resultados:
mysql> use test
Database changed
mysql> DROP TABLE IF EXISTS emailTable;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE `emailTable` (
-> `id` mediumint(9) NOT NULL auto_increment,
-> `email` varchar(200) NOT NULL default '',
-> PRIMARY KEY (`id`)
-> ) ENGINE=MyISAM;
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO emailTable (email) VALUES
-> ('redwards@gmail.com'),
-> ('redwards@gmail.com'),
-> ('redwards@gmail.com'),
-> ('redwards@gmail.com'),
-> ('rolandoedwards@gmail.com'),
('rolandoedwards@comcast.net');
SELECT * FROM emailTable;
-> ('rolandoedwards@gmail.com'),
-> ('rolandoedwards@gmail.com'),
-> ('red@gmail.com'),
-> ('red@gmail.com'),
-> ('red@gmail.com'),
-> ('rolandoedwards@gmail.com'),
-> ('rolandoedwards@gmail.com'),
-> ('rolandoedwards@comcast.net'),
-> ('rolandoedwards@comcast.net'),
-> ('rolandoedwards@comcast.net');
Query OK, 15 rows affected (0.00 sec)
Records: 15 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM emailTable;
+----+----------------------------+
| id | email |
+----+----------------------------+
| 1 | redwards@gmail.com |
| 2 | redwards@gmail.com |
| 3 | redwards@gmail.com |
| 4 | redwards@gmail.com |
| 5 | rolandoedwards@gmail.com |
| 6 | rolandoedwards@gmail.com |
| 7 | rolandoedwards@gmail.com |
| 8 | red@gmail.com |
| 9 | red@gmail.com |
| 10 | red@gmail.com |
| 11 | rolandoedwards@gmail.com |
| 12 | rolandoedwards@gmail.com |
| 13 | rolandoedwards@comcast.net |
| 14 | rolandoedwards@comcast.net |
| 15 | rolandoedwards@comcast.net |
+----+----------------------------+
15 rows in set (0.00 sec)
mysql> CREATE TABLE emailUnique LIKE emailTable;
Query OK, 0 rows affected (0.04 sec)
mysql> ALTER TABLE emailUnique ADD UNIQUE INDEX (email);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> INSERT IGNORE INTO emailUnique SELECT * FROM emailTable;
Query OK, 4 rows affected (0.01 sec)
Records: 15 Duplicates: 11 Warnings: 0
mysql> SELECT * FROM emailUnique;
+----+----------------------------+
| id | email |
+----+----------------------------+
| 1 | redwards@gmail.com |
| 5 | rolandoedwards@gmail.com |
| 8 | red@gmail.com |
| 13 | rolandoedwards@comcast.net |
+----+----------------------------+
4 rows in set (0.00 sec)
mysql> ALTER TABLE emailTable RENAME emailTable_old;
Query OK, 0 rows affected (0.03 sec)
mysql> ALTER TABLE emailUnique RENAME emailTable;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TABLE emailTable_old;
Query OK, 0 rows affected (0.00 sec)
mysql>
Conforme mostrado, a emailTable conterá a primeira ocorrência de cada endereço de email e o ID original correspondente. Para este exemplo:
- Os IDs 1 a 4 têm redwards@gmail.com, mas apenas 1 foi preservado.
- Os IDs 5-7,11,12 têm rolandoedwards@gmail.com, mas apenas 5 foram preservados.
- Os IDs 8 a 10 têm red@gmail.com, mas apenas 8 foram preservados.
- Os IDs 13 a 15 têm rolandoedwards@comcast.net, mas apenas 13 foram preservados.
CAVEAT: Respondi a uma pergunta semelhante a essa sobre exclusão de tabela por meio de uma abordagem de tabela temporária .
De uma chance !!!