After the implode()
call, add:
$sql .= " ON DUPLICATE KEY UPDATE colB = VALUES(colB)";
Assuming colA
has a unique key, then whenever that column already exists, this will set colB
to the new colB
from that row of the INSERT
, and leave all the other columns unchanged.
http://dev.mysql.com/doc/refman/5.5/en/insert-on-duplicate.html
It has the following example that shows that each duplicate row is handled independently.
INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);
That statement is identical to the following two statements:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=3;
INSERT INTO table (a,b,c) VALUES (4,5,6)
ON DUPLICATE KEY UPDATE c=9;