现在,我正在尝试从表中的列中获取最高值然后递增它(+1),然后将同一列的另一个记录更新为该数字。 我用PHP编写脚本,将变量$ SQL发送给MySQL。
$SQL="
UPDATE `router`
SET `Line Order`= (SELECT 1 + IFNULL(MAX(`Line Order`), 0) FROM `router`)
WHERE `RN`=?
";
$stmt = $GLOBALS['mySQLConnection']->prepare($SQL);
echo $GLOBALS['mySQLConnection']->error;
错误是:您无法在FROM子句中为更新指定目标表'router' 如果不在php中编写单独的查询,我们怎么想这样做呢?
答案 0 :(得分:1)
尝试此查询:
UPDATE router AS r1
INNER JOIN (
SELECT 1 + IFNULL(MAX(`Line Order`), 0) AS new FROM `router`) AS r2
SET `Line Order` = r2.new
WHERE `RN` = ?