仅当新数据不存在时才插入

时间:2019-06-02 09:25:19

标签: php sqlite

为什么这不起作用?我试图仅在不符合我的要求时才插入新数据。

$stm= "INSERT INTO `orders` (userid, username, quantity, time, image, uprice, tprice) 
        VALUES
        (:userid, :username, :quantity, :timee, :image, :uprice, :tprice) 
        WHERE NOT EXISTS (select username from orders where username=:username and image=:image)";

1 个答案:

答案 0 :(得分:1)

使用INSERT .... VALUES代替WHERE,不允许使用INSERT .... SELECT子句:

$stm= "INSERT INTO `orders` (userid, username, quantity, time, image, uprice, tprice) 
       SELECT :userid, :username, :quantity, :timee, :image, :uprice, :tprice
       WHERE NOT EXISTS (select username from orders where username=:username and image=:image)";