我想知道为什么 MySQL 不允许在存储过程中锁定表。
我的存储过程中有以下 SQL 语句:
-- Total amount of money for comments
start transaction;
select @num_comments := count(*)
from `comment` c
where
c.user_id = user_id and
c.payment_rejection = 'NO' and
c.is_recorded = 0;
update `user` u set account_balance += u.comment_price * @num_comments where u.user_id = user_id;
update `comment` c set is_recorded = 1 where c.user_id = user_id and c.payment_rejection = 'NO' and c.is_recorded = 0;
commit;
所以我必须锁定表comment
以防止对其进行任何写入,因为它可能会导致在第一个 SQL 语句中选择的行数与实际更新的行数不同。