這是我以前的question。我無法解決問題。我更新我的代碼,並間接使用GROUP BY
。但同樣的問題表明。子查詢返回重複值。其實我做了一個觸發器,因此,我認爲,如果顯示的觸發,這將是清楚明白錯誤#1242子查詢在更新時返回多於一行
--
-- Triggers `product_purchases`
--
DROP TRIGGER IF EXISTS `change_stock_when_purchases`;
DELIMITER //
CREATE TRIGGER `change_stock_when_purchases` AFTER INSERT ON `product_purchases`
FOR EACH ROW BEGIN
DECLARE X INTEGER;
SET X = (SELECT product_size FROM product_stock WHERE product_id = NEW.product_id);
IF NEW.product_size =X THEN
UPDATE
product_stock as S1
INNER JOIN
(
SELECT
S.product_stock_id,I.quantity
FROM
product_stock AS S
INNER JOIN
product_purchase_item AS I
ON
S.product_id=I.product_id AND S.product_size=I.product_size
INNER JOIN
product_purchases AS P
ON
I.product_purchase_item_id=P.product_purchase_item_id
WHERE
P.product_purchase_id=NEW.product_purchase_id
GROUP BY
S.product_stock_id
)
AS M
ON
S1.product_stock_id=M.product_stock_id
SET
S1.product_size_quantity=S1.product_size_quantity+M.quantity;
ELSE
INSERT INTO
product_stock (product_id,product_size,product_size_quantity)
SELECT
product_id,product_size, quantity
FROM
product_purchase_item
WHERE
product_purchase_item_id=NEW.product_purchase_item_id;
END IF;
END
//
DELIMITER ;
問題顯示,當它在你的代碼上product_stock
一些樣本數據會有所幫助。一個問題 - 在你的內連接where子句應該包含product_too?即像P.product_purchase_id = NEW.product_purchase_id AND s.product_id = NEW.product_id。希望這有助於 –
@AndaIancu我還加\t S.product_id = NEW.product_id \t \t \t和 \t \t \t S.product_size = NEW.product_size但同樣的問題 – Imran
考慮提供表的模式和樣本數據。這可能會大大提高您獲得您正在尋找答案的機會。 – peterm