Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.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
如何在SQLite3中更新满足递增数字条件的记录?_Sql_Sqlite_Sql Update - Fatal编程技术网

如何在SQLite3中更新满足递增数字条件的记录?

如何在SQLite3中更新满足递增数字条件的记录?,sql,sqlite,sql-update,Sql,Sqlite,Sql Update,这是一个重复的问题,但我需要一个方法,将与SQLite3的工作 从原来的问题剪下: 剪断 我在博士后中有这样一张表: Id Name local_site_id local_id 1 A 2 2 B 2 3 C 1 4 D 2 5 E 1 如何使用SQL查询将表更新到此中: Id Name local_site_id local

这是一个重复的问题,但我需要一个方法,将与SQLite3的工作

从原来的问题剪下:

剪断

我在博士后中有这样一张表:

Id    Name    local_site_id    local_id
1     A       2                
2     B       2
3     C       1
4     D       2
5     E       1
如何使用SQL查询将表更新到此中:

Id    Name    local_site_id    local_id
1     A       2                1
2     B       2                2
3     C       1                
4     D       2                3
5     E       1                
现在,所有记录的local_id字段都是空的。我只想对具有
local\u site\u id=2
的行使用从1开始递增的数字更新local\u id值是否可以使用SQL

端剪

我在那里尝试了这个命令,但它不适用于SQLite3

update T set local_id=s.rn 
from (select id,row_number() over(order by id) as rn from T where local_site_id=2) s
where T.id=s.id;
如何在SQLite3中实现这一点?

这应该可以做到:

.mode column
.headers on

create table T (Id, Name, local_site_id, local_id);

insert into T values
    (1, 'A', 2, null),
    (2, 'B', 2, null),
    (3, 'C', 1, null),
    (4, 'D', 2, null),
    (5, 'E', 1, null);

update T set local_id = (
    select 
        case local_site_id
            when 2 then (select count(*) 
                         from T t2 
                         where t2.id <= t1.id and local_site_id=2)
            else null
        end
    from T as t1 where T.id=t1.id);

select * from T;

我自己找到了一条路。我创建了一个临时表,然后使用临时表的“ROWID”内部列来更新原始表

create table temptable as select id from tablename where local_site_id=2;

update tablename 
    set local_id=(select ROWID from temptable where temptable.id=tablename.id)
    where exists (select ROWID from temptable where temptable.id=tablename.id);

但我会接受Ludo的回答,因为它不涉及创建新表。

太棒了。。我创建了一个新的表格,结果弄得一团糟。。但我会接受你的正确答案:)
create table temptable as select id from tablename where local_site_id=2;

update tablename 
    set local_id=(select ROWID from temptable where temptable.id=tablename.id)
    where exists (select ROWID from temptable where temptable.id=tablename.id);