我有一张桌子,上面有一个房间列表,每个房间都分配了一个房间号码。我想删除最高的房间号码:
DELETE FROM rooms WHERE roomNb = (SELECT max(roomNb) FROM rooms LIMIT 1)
我希望这可行,但当我尝试在PHP中执行此查询时,我得到了这个mysqli_error:
string(64) "You can't specify target table 'rooms' for update in FROM clause"
答案 0 :(得分:2)
尝试使用join,
DELETE a
FROM rooms a
INNER JOIN (SELECT max(roomNb) maxroom FROM rooms) b
ON a.roomNb = b.maxroom
答案 1 :(得分:2)
我没有看到使用子查询/连接的原因...为什么不在ORDER BY
子句之后将行从表顶移开?
DELETE FROM rooms ORDER BY roomNb DESC LIMIT 1