根据条件更新MySQL记录

时间:2015-01-27 10:31:35

标签: php mysql

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 个答案:

答案 0 :(得分:0)

<强> 1。方法1: 为此,您必须首先获取表中存在的所有记录的记录集,然后针对您的阵列检查每个记录,以确定它们是否必须存在。

<强> 2。方法2:将存在于数组中的skus放入一个表(Say ValidLookupHelper)中,然后运行一个带有NOT IN的MySql查询,该查询在原始数据表中找到,但不在ValidLookupHelper中。

相关问题