Вы можете использовать INSERT ... ON DUPLICATE KEY UPDATE до INSERT
новых строк и UPDATE
существующих строк.
Пример:
INSERT INTO t1 (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE a=1,b=2,c=3;
Вы должны построить оператор DELETE
, используя несколько трюков в php.
Пример:
<?php
$data = array(
array( 'a'=>1, 'b'=>2, 'c'=>3 ),
array( 'a'=>4, 'b'=>5, 'c'=>6 ),
array( 'a'=>7, 'b'=>8, 'c'=>9 ),
);
$condition = implode( ',', array_map( function($temp){return sprintf("('%s','%s','%s')",$temp['a'],$temp['b'],$temp['c']);},$data));
$sql = "DELETE FROM t1 WHERE (a,b,c) NOT IN (" . $condition . ");";
echo $sql; // DELETE FROM t1 WHERE (a,b,c) NOT IN (('1','2','3'),('4','5','6'),('7','8','9'));
// OR
$condition1 = implode( ',', array_map( function($temp){return sprintf("'%s'",$temp['a']);},$data));
$sql1 = "DELETE FROM t1 WHERE a NOT IN (" . $condition1 . ");";
echo $sql1; // DELETE FROM t1 WHERE a NOT IN ('1','4','7');
?>