MySql: check and remove duplicate rows

duplicationMySQL

  • How can I check if a table contains duplicate rows, with the same value of the couple (date, time) ?
  • How can I remove all but one row ?
  • Id is a sequential number that I increment after every post, how can I restore it after removing a row ?

the table tableTen is:

+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id        | int(11)     | YES  |     | NULL    |       |
| symbol    | varchar(25) | YES  |     | NULL    |       |
| date      | date        | YES  | MUL | NULL    |       |
| time      | time        | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

Best Answer

1. How can I check if a table contains duplicate rows, with the same value of the couple (date, time) ?

SELECT *  FROM db.table HAVING COUNT(symbol) > 1 

2. How can I remove all but one row ?

a) If you want to keep the row with the lowest id value:

DELETE n1 FROM table n1, table n2 WHERE n1.id > n2.id AND n1.symbol= n2.symbol

b) If you want to keep the row with the highest id value:

DELETE n1 FROM table n1, table n2 WHERE n1.id < n2.id AND n1.symbol= n2.symbol

3. Id is a sequential number that I increment after every post, how can I restore it after removing a row ?

Please try http://befused.com/mysql/reset-auto-increment