我想从main_table中获取持续时间列中的值,从列中的每个值中减去 1(一),然后用差值更新同一列(减去后的结果),例如
main_table (before any operation)
--------------------------------
id duration
1 x
2 y
3 z
main_table (after update operation)
--------------------------------
id duration
1 x - 1
2 y - 1
3 z - 1
这是我到目前为止尝试过但失败了
UPDATE main_table mt, (SELECT duration - 1 AS remaining
FROM main_table mt1
) mt1
SET mt.duration = mt1.remaining
WHERE mt.id = mt1.id
您不需要子查询,可以使用单个更新语句来完成
update main_table
set
duration = duration - 1
您可以像这样直接使用您的列,这将根据需要更新您的列值。检查一下,可能会对你有所帮助。
UPDATE main_table mt
SET mt.duration = mt.duration -1