Error 1062 in mysql

453 Views Asked by At
INSERT INTO order SELECT * FROM order WHERE date > DATE_SUB(NOW(), INTERVAL 6 MONTH);

I am getting Error Code: 1062 Duplicate entry '5890738' for key 'PRIMARY'

How to resolve this as I want only 6 months data but this table contains 6 years data?

2

There are 2 best solutions below

2
Imanez On

What you get as error means that you try to insert id (which a primary and unique key) twice times, to solve that you must use something like that :

INSERT INTO order(all your columns except Id_column) SELECT all your columns except Id_column FROM order WHERE date > DATE_SUB(NOW(), INTERVAL 6 MONTH);

Note : Id must be auto-increment column

0
Praphull Priyadarshi On

Use MySQL query:

INSERT INTO order(all your columns except Id_column)   
SELECT all your columns except Id_column   
FROM order WHERE date > DATE_SUB(NOW(), INTERVAL 6 MONTH);