MySQL InnoDB Global Var?

时间:2013-03-16 17:01:36

标签: mysql sql sql-update innodb

我想执行此代码: 我们的想法是获取具有最低MID未使用的项目(mutex = 0)。为了防止其他线程读取此行FOR UPDATEUPDATE减少mutex

START TRANSACTION;
SELECT MID as m, sender, receiver, content FROM queue WHERE mutex = 0 ORDER BY MID ASC limit 1 FOR UPDATE; 
UPDATE queue set mutex = -1 WHERE MID = m;
COMMIT;

m仅在执行SELECT行时才知道。如何在不中断查询的情况下在下一个查询中访问它,这将停止TRANSACTION

P.S。:我知道互斥锁不是我用它的东西。

1 个答案:

答案 0 :(得分:1)

您可以使用变量:

START TRANSACTION;
SELECT @m:=MID, sender, receiver, content FROM queue WHERE mutex = 0 ORDER BY MID ASC limit 1 FOR UPDATE; 
UPDATE queue set mutex = -1 WHERE MID = @m;
COMMIT;