SQL:不退出的地方 - 仅在不存在时插入

时间:2017-02-12 11:18:35

标签: php mysql sql

我想仅在尚未退出的情况下插入值。我的表名为wp_all_import,我的值称为pidpriceprice_oldshoplink

有人可以告诉我这里有什么不对吗? 因为他插入了价值但他们已经退出......

INSERT INTO `wp_all_import` ('$pid', '$price', '$price_old', '$link', '$shop') 
        SELECT 'pid', 'price', 'price_old', 'shop', 'link' FROM `wp_all_import` 
        WHERE NOT EXISTS (SELECT * FROM `wp_all_import` 
        WHERE (pid = '$rpid') AND (price = '$rprice') AND (price_old = '$rprice_old') AND (shop = '$rshop') AND (link = '$rlink'))

问候,谢谢!

1 个答案:

答案 0 :(得分:0)

我没有真正了解您的代码,但我认为这正是您所寻找的:

我认为变量(以$ sign开头)表示您要插入的值

INSERT INTO wp_all_import(pid, price, price_old, shop, link)
SELECT * FROM (SELECT $pid, $price, $price_old, $shop, $link) AS tmp
WHERE NOT EXISTS (
    SELECT * FROM wp_all_import 
    WHERE pid= $pid AND price = $price 
    AND price_old = $price_old AND link = $link
    AND shop = $shop) LIMIT 1;