1. 程式人生 > >sqlite多表關聯update

sqlite多表關聯update

括號 多表 sel clas sqlite OS 支持 pda IT

sqlite數據庫的update多表關聯更新語句,和其他數據庫有點小不一樣

比如:在sql server中:

用table1的 id 和 table2的 pid,關聯table1 和 table2 ,將table2的num字段的值賦給table1的num字段

update table1 
set num1 = t2.num2
FROM table1 t1 INNER JOIN table2 t2
ON t1.id=t2.pid;

很容易就關聯起來了

sqlite卻不支持這種關聯,可以這樣:

(1)set時,要將table2的num2的值賦給table1的num1字段,要select一下table2,並在括號關聯起來

update table1
set  num1 = (select num2 from table2 where table2.pid=table1.id)
where...

更新多個字段時:

update table1
set  num1 = (select num2 from table2 where table2.pid=table1.id),
num11 = (select num22 from table2 where table2.pid=table1.id)
where...

(2)where時,也一樣,比如我就將上面的改一下

update table1
set  num = 99
where table1.id=(select pid from table2 where table2.pid=table1.id)

sqlite多表關聯update