2014-11-21 100 views
0

我想從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 

建議你們實現這個!

回答

1

你不需要子查詢這一點,可以用一個更新語句

update main_table 
set 
duration = duration - 1 
1

您可以直接使用您的列這樣做,因爲你需要這將更新您的列值。 檢查它,可能會幫助你。

UPDATE main_table mt 
SET mt.duration = mt.duration -1