1

I want to do an INSERT SELECT query like this:

INSERT INTO `tableName` (SELECT * FROM `anotherTable`) 

The problem is when it finds a duplicate value the whole thing will stop, really I just want it to continue and skip the duplicates.

I know I can do this via the cmd line by using the -f parameter to force it, but I want to do it with PHP. Is there a force or ignore errors option somewhere in the PHP mysql_* functions?

Cœur
  • 34,719
  • 24
  • 185
  • 251
Drahcir
  • 11,382
  • 18
  • 77
  • 125
  • 1
    Have a look at this INSERT IGNORE -> http://stackoverflow.com/questions/548541/insert-ignore-vs-insert-on-duplicate-key-update – Manse Nov 29 '11 at 15:55

2 Answers2

2

You need to add ON DUPLICATE KEY IGNORE :

INSERT INTO `tableName` (SELECT * FROM `anotherTable`) ON DUPLICATE KEY IGNORE

Info here-> http://dev.mysql.com/doc/refman/5.5/en/insert.html

Manse
  • 37,343
  • 10
  • 82
  • 108
1

Use INSERT IGNORE

INSERT IGNORE INTO `tableName` (SELECT * FROM `anotherTable`) 
Martin.
  • 10,271
  • 3
  • 40
  • 67