ERROR 1172 (42000): 结果在 mysql 存储过程中包含多于一行

Posted

技术标签:

【中文标题】ERROR 1172 (42000): 结果在 mysql 存储过程中包含多于一行【英文标题】:ERROR 1172 (42000): Result consisted of more than one row on mysql stored procedure 【发布时间】:2020-04-12 05:14:55 【问题描述】:

我收到错误 1172 (42000):当我使用 CALL submitOrder(10, 100, 1, @OrderId); 时,结果在我的存储过程中包含多于一行;

我的代码出了什么问题?目标是使用该过程将新行插入到订单表中,并将订单详细信息上的新行插入到 order_items 表中。

DELIMITER //

CREATE PROCEDURE submitOrder(
IN customerId INT, 
IN productId INT, 
IN qty INT, 
OUT orderId INT)
BEGIN
DECLARE orderId, storeId, staffId, qty, customerId, productId INT; 
DECLARE listPrice DECIMAL(10,2); 
DECLARE discount DECIMAL(4,2);

SELECT MAX(order_id)+1 FROM orders INTO @orderId;

SELECT s.store_id
FROM stocks AS s
INNER JOIN products AS p USING (product_id)
WHERE p.product_id = s.product_id
ORDER BY s.quantity DESC
LIMIT 1
INTO @storeId;

SELECT staffs.staff_id 
FROM staffs
INNER JOIN stores
WHERE staffs.store_id = stores.store_id
LIMIT 1
INTO @staffID; 

SET @qty = 1;

SELECT products.product_id
FROM products
WHERE products.product_id = productId;

SELECT products.list_price
FROM products
INTO @listPrice;

INSERT INTO orders VALUES (
@orderId, @customerId, 1, CURDATE(), ADDDATE(CURDATE(), INTERVAL 7 day), NULL, @storeId, @staffId);

INSERT INTO order_items
VALUES (@orderId, 1, @productId, @qty, @listPrice,0);

END//

DELIMITER ;

【问题讨论】:

您不使用AUTO_INCREMENT 作为order_id 有什么原因吗? 【参考方案1】:

您忘记将WHERE 子句和LIMIT 1 放入设置$listPrice 的查询中。你也忘了在任何地方设置@productId

您可以在同一个查询中同时执行这两项操作。

SELECT product_id, list_price
FROM products
WHERE product_id = productId
LIMIT 1
INTO @productId, @listPrice;

我不确定您为什么需要 @product_id 变量,因为它将与 productId 相同。

【讨论】:

以上是关于ERROR 1172 (42000): 结果在 mysql 存储过程中包含多于一行的主要内容,如果未能解决你的问题,请参考以下文章

ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'

mysql ERROR 1044 (42000): Access denied for user ''@'localhost' to database

[故障解决]Mysql爆出ERROR 1044 (42000)的错误怎么办?

ERROR 1248 (42000): Every derived table must have its own alias

ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes

为啥我在程序上收到 ERROR 1064 (42000) mysql 错误?