我有下表,我尝试使用以下查询增加 ref 列值。
orderid name age address ref
1 A 22 a1 1
2 B 21 b1 0
update table1
set ref =
'(SELECT (MAX(ref) + 1) AS max_ref FROM table1)'
where name= 'B'
但如果是 2,它总是设置为 0。
谢谢。
最佳答案
根据 MySQL doc :
Currently, you cannot update a table and select from the same table in a subquery.
但是很容易绕过:
UPDATE table1
SET ref = (
SELECT m.max_ref FROM (
SELECT (max(ref) + 1) AS max_ref FROM table1) m)
WHERE name= 'B'
关于mysql - 内部加入 max 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6321609/