更新和删除行时防止更新表

时间:2013-02-26 13:04:17

标签: mysql

我正在为一个小游戏建立一个配对系统。我保存所有搜索对手的玩家的表格如下:

lobby
----------
userID
points
range
opponentID

脚本每5秒钟尝试在一定范围内找到一个对手。

UPDATE lobby SET
    opponentID = {myUserID}
WHERE points >= {myPoints} - {myRange} AND points <= {myPoints} + {myRange}
    AND opponentID = 0 AND userID != {myUserID}
ORDER BY ABS(points - {myPoints}) ASC
LIMIT 1

如果有受影响的行,我会从大厅中移除当前玩家并获得对手ID:

DELETE FROM lobby
WHERE userID = {myUserID}

SELECT userID FROM lobby
WHERE opponentID = {myUserID}

现在我担心有人在UPDATE和DELETE之间分配当前用户。我怎么能阻止这个?我可以将UPDATE和DELETE结合起来吗?

0 个答案:

没有答案