DB包含:
sku price available
0113A5 300 1
0213A5 250 1
PHP 伪代码:
['0113A5' => ... , '0313A5' => ... ]
loop:
if exists in DB ['0113A5']:
update
if not exists in DB ['0113A5']:
create
delete all other records in the table (in this case 0213A5 which isn't in the array)
我可以通过以下方式INSERT
(更新)记录:
INSERT INTO product (sku, price, available)
VALUES ('{$sku_safe}', '{$price}', '{$available}')
ON DUPLICATE KEY UPDATE
price = VALUES(price),
available = VALUES(available)
.. 但我不确定如何从表中删除不匹配的记录。
1. 方法1:为此,您必须首先获取表中存在的所有记录的记录集,然后对照数组检查每个记录,了解它们是否需要存在。
2. 方法 2:将数组中存在的 SKU 放入一个表中(比如说 ValidLookupHelper),然后运行一个 MySql 查询,其中包含一个 NOT IN 表示,在原始数据表中找到,但不在 ValidLookupHelper 中找到。