Thursday, 28 June 2012

Delete Duplicate Entry from Table Using ( Two Condition )


SELECT * FROM chat;
+----+----------+
| id | name     |
+----+----------+
| 1  | rajneesh |
| 2  | rajneesh |
| 3  | gautam   |
| 4  | rajneesh |
| 5  | gautam   |
| 6  | gautam   |
+----+----------+
 
DELETE A1 FROM chat A1, chat A2 WHERE A1.id > A2.id AND A1.name = A2.name

if you want to keep the row with the lowest id value OR

DELETE A1 FROM chat A1, chat A2 WHERE A1.id < A2.id AND A1.name = A2.name

if you want to keep the row with the highest id value.