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
  • 37,241
  • 25
  • 195
  • 267
Drahcir
  • 11,772
  • 24
  • 86
  • 128
  • 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,765
  • 10
  • 83
  • 108
1

Use INSERT IGNORE

INSERT IGNORE INTO `tableName` (SELECT * FROM `anotherTable`) 
Martin.
  • 10,494
  • 3
  • 42
  • 68