當我執行此查詢時,我收到此錯誤訊息“錯誤代碼:1172。結果由多行組成”
CREATE DEFINER=`root`@`localhost` PROCEDURE `un_follow`(
user_been_following_id int,
user_following_id int
)
BEGIN
declare id int;
select following_id into id from user_following
where user_been_following_id = user_been_following_id
and user_following_id = user_following_id;
delete from user_following
where following_id = id;
END
id 是下表的主鍵有什么幫助嗎?
uj5u.com熱心網友回復:
您的區域變數與表列同名。這樣你就永遠不會將區域變數與列進行比較,而總是將區域變數與區域變數本身進行比較。
您的查詢需要恰好回傳一行以提供 id 變數
select following_id into id from user_following
where user_been_following_id = user_been_following_id
and user_following_id = user_following_id;
user_been_following_id 和 user_following_id 在所有實體中都被解釋為區域變數,所以這翻譯成
select following_id into id from user_following
where 1 = 1
and 1 = 1;
其中回傳 user_following 的所有行。要解決此問題,請重命名您的區域變數,例如
CREATE DEFINER=`root`@`localhost` PROCEDURE `un_follow`(
local_user_been_following_id int,
local_user_following_id int
)
BEGIN
declare id int;
select following_id into id from user_following
where user_been_following_id = local_user_been_following_id
and user_following_id = local_user_following_id;
delete from user_following
where following_id = id;
END
(假設您在表 user_following 上沒有名為 local_user_been_following_id 或 local_user_following_id 的列)
另見此處: https ://dev.mysql.com/doc/refman/8.0/en/local-variable-scope.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/535370.html
標籤:数据库数据库
