我有下表:
Table final
-id INTEGER PRIMARY KEY
-addcounter TEXT NOT NULL
-secondaryID TEXT NOT NULL
使用以下格式:
1 0 1
2 1 null
3 1 null
4 0 null
5 0 null
所以基本上,on(行X的secondaryID)=(行X-1的secondaryID)+(行X-1的addcounter)
因此上表应该导致:
1 0 1
2 1 1
3 1 2
4 0 3
5 0 3
答案 0 :(得分:1)
UPDATE final
SET secondaryID = (SELECT secondaryID
FROM final
ORDER BY id
LIMIT 1) +
(SELECT SUM(addcounter)
FROM final AS f2
WHERE id < final.id)
WHERE secondaryID IS NULL