Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用以前的行更新行_Sql_Sqlite_Sql Update_Subquery - Fatal编程技术网

Sql 使用以前的行更新行

Sql 使用以前的行更新行,sql,sqlite,sql-update,subquery,Sql,Sqlite,Sql Update,Subquery,我有一个表,列“id”是自动递增的。在一些记录中,我的值为零。我想通过它们以前的行(这里previous的意思是id-1)来更新它们。 我该怎么做 此查询返回那些值为零的记录: SELECT * FROM myTable WHERE col1 = 0 返回: id col1 col2 ..... coln 15 0 0 0 23 0 0 0 您可以使用相关子查询: update mytable t set col1 =

我有一个表,列“id”是自动递增的。在一些记录中,我的值为零。我想通过它们以前的行(这里previous的意思是id-1)来更新它们。 我该怎么做

此查询返回那些值为零的记录:

SELECT * FROM myTable
WHERE col1 = 0 
返回:

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

您可以使用相关子查询:

update mytable t
set col1 = (select t1.col1 from mytable t1 where t1.id = t.id - 1)
where col1 = 0