用以前的行更新行
Update rows with previous rows
我有一个 table,其列 'id' 是自动递增的。在某些记录中,我的值为零。我想通过他们之前的行更新它们(这里之前的意思是 id - 1)。
我该怎么做?
此查询returns那些值为零的记录:
SELECT * FROM myTable
WHERE col1 = 0
哪个returns:
id col1 col2 ..... coln
15 0 0 0
23 0 0 0
您可以使用相关子查询:
update mytable t
set col1 = (select t1.col1 from mytable t1 where t1.id = t.id - 1)
where col1 = 0
我有一个 table,其列 'id' 是自动递增的。在某些记录中,我的值为零。我想通过他们之前的行更新它们(这里之前的意思是 id - 1)。 我该怎么做?
此查询returns那些值为零的记录:
SELECT * FROM myTable
WHERE col1 = 0
哪个returns:
id col1 col2 ..... coln
15 0 0 0
23 0 0 0
您可以使用相关子查询:
update mytable t
set col1 = (select t1.col1 from mytable t1 where t1.id = t.id - 1)
where col1 = 0