sqlite update each row of a column with the result of a select (again) -
need add column table fill computed values select (from orignal table itself) this
create table yo (ad integer primary key, pa integer, pd integer); insert yo values (1,1,1),(2,1,3),(3,1,4),(4,3,5),(5,4,2),(6,3,8),(7,1,9),(8,6,7),(9,3,6); .header on .mode column yo; select * yo; ad pa pd ---------- ---------- ---------- 1 1 1 2 1 3 3 1 4 4 3 5 5 4 2 6 3 8 7 1 9 8 6 7 9 3 6 the 'ad' col kinda address, 'pa' col parent address, 'pd' col kinda id or value row. i'd add col named 'pp' 'parent id' computed this
select yo2.pd yo yo1 join yo yo2 on yo2.ad=yo1.pa; pd ---------- 1 1 1 4 5 4 1 8 4 so i'd setup 'pp' whit output tried must miss somethings
alter table yo add column pp integer; update yo set pp = (select yo2.pd pp yo yo1 join yo yo2 on yo2.ad=yo1.pa); ad pa pd pp ---- --- --- ---- 1 1 1 1 2 1 3 1 3 1 4 1 4 3 5 1 5 4 2 1 6 3 8 1 7 1 9 1 8 6 7 1 9 3 6 1 does want achieve doable, othre link on similar proble don't understand answers, , found tiny case this
create table t1 (a int, b int); insert t1 values (1,0),(2,0); create table t2 (a int, c int); insert t2 values (1,101),(2,102); update t1 set b = (select c t2 t2.a = t1.a) t1.b = 0; this 1 update correctly why select differ later one?
thanx advise
phi
i found way achieve want using temp table this
create temp table ziv select yo1.ad,yo2.pd pp yo yo1 join yo yo2 on yo2.ad=yo1.pa update yo set pp=(select temp.ziv.pp temp.ziv temp.ziv.ad=yo.ad) select * yo ad pa pd pp --- --- --- --- 1 1 1 1 2 1 3 1 3 1 4 1 4 3 5 4 5 4 2 5 6 3 8 4 7 1 9 1 8 6 7 8 9 3 6 4 now wonder can done 1 update (no temp table) thanx in advance
cheers
phi
Comments
Post a Comment