【发布时间】:2012-08-30 13:51:18
【问题描述】:
这是 Postgres Update records that satisfies a condition with incrementing number 的这个问题的副本,但我需要一种适用于 SQLite3 的方法。
从原始问题中截取:
剪辑
我在 postgres 中有一个这样的表:
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 字段都是空的。我想使用从 1 开始的递增数字更新 local_id 值,仅用于具有 local_site_id=2 的行是否可以使用 SQL?
结束片段
我从那里的answer 尝试了这个命令,但它不适用于 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 中实现这一点?
【问题讨论】:
标签: sql sqlite sql-update