MYSQL:
DELIMITER $$create TRIGGER `unit` beforeinsert ON `cart` FOR each row
BEGIN
IF(EXISTS
(
SELECT 1
FROM `cart`
WHERE `USER` = new.USER
AND `item` = new.item)) THEN
UPDATE `cart`
SET `unit` = new.unit,
`quantity` = new.quantity
WHERE `USER` = new.USER
AND `item` = new.item;ENDIF;
END
$$ delimiter ;
错误:
Can't update table 'cart' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
当我尝试插入数据时,如果已经存在则显示错误。如果商品已经在购物车中,我想更新数据。我该如何解决这个错误?
答案 0 :(得分:0)
您无法在INSERT触发器内部进行更新,而不是执行INSERT。您需要在代码中插入之前检查记录,或者查看使用ON DUPLICATE KEY UPDATE语句:
INSERT INTO t1 (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;