mysql数据库值进行比较数组值与PDO

mysql数据库值进行比较数组值与PDO

问题描述:

我不能比较这阵mysql数据库值进行比较数组值与PDO

$products = []; 
foreach (array_keys($match[2]) as $idx) 
{ 
    $tagliaprodotto = rtrim(substr($match[2][$idx],1)); 
    $tagliaquantita = ltrim($match[3][$idx],'0'); 
    $products[] = [ 
     'product' => $tagliaprodotto, 
     'quantity' => $tagliaquantita, 
     'terminal' => $match[4][$idx] 
        ]; 
} 

与数据库内的值的值!我应该这样做吗?

我想比较,我有在数据库内的元素与数组的值,使一些if为:

- 如果产品不存在于数据库中:

INSERT INTO table (product, quantity, terminal, date) VALUES (:product, :quantity, :terminal, NOW())

- 如果数据库记录完美匹配:产品,数量和终端:Do nothing

- 如果数据库记录包含:相同产品但数量不同和/ o [R终端:

UPDATE table SET quantity=:quantity, terminal=:terminal

+0

https://*.com/questions/23305300/check-if-username-exists-pdo –

+0

我没有单个变量$ user ='userName'; – Squalo

+0

这是演示例子,所以你可以更新你的代码,就像它 –

我假设你指数product,否则这得到棘手。使用的ON DUPLICATE KEYIF()组合:

INSERT INTO table (product, quantity, terminal, date) 
    VALUES (:product, :quantity, :terminal, NOW()) 
ON DUPLICATE KEY UPDATE 
    quantity = IF(quantity != :quantity AND terminal = :terminal, :quantity, quantity), 
    terminal = IF(quantity = :quantity AND terminal != :terminal, :terminal, terminal) 

如果有不product指数(或任何指定的列),该UPDATE将永远不会触发。如果查询中存在多个列的索引,则会出现您不打算更新的行得到更新的问题,尤其是在索引不唯一的情况下。

+0

谢谢,但我需要更新日期,但只有当数量或终端更改或产品不存在时 - – Squalo

+0

这是一个相对容易的除了查询您应该能够添加的更新部分。它仍然会使用'IF()',并且看起来与数量和终端设置类似,但稍长一些。给它一个镜头,如果你仍然需要帮助,我会带你通过它。 – Spencer

+0

一个答案:1如果数量不同,终端不同它不起作用它是正确的? – Squalo