当客户还没有购买产品的记录时,我正在尝试执行INSERT INTO查询.
我已经尝试了下面的sql,但似乎没有用.
$queryAddPurchase = "INSERT INTO purchase (UserID,Product,Price) VALUES ('$userID','$product','$price') WHERE NOT EXISTS(SELECT Product FROM purchase WHERE Product = '$product' AND UserID = '$userID')";
我收到的错误如下:
You have an error in your sql Syntax; check the manual that corresponds to your MysqL server version for the right Syntax to use near ‘WHERE NOT EXISTS(SELECT Product FROM purchase ‘ at line 3
任何建议将不胜感激!
假设用户可能只购买每种产品中的一种(永远和所有产品).
ALTER TABLE purchase ADD UNIQUE KEY (`UserID`,`Product`); -- run this just once. this changes the table INSERT IGNORE INTO purchase (UserID,Price) VALUES ('$userID','$price');
请注意,这会阻止他多次购买任何产品,这可能不是理想的结果.