如何在SQLite上连接表时进行更新?

时间:2009-04-21 16:19:46

标签: sqlite join sql-update

我试过了:

UPDATE closure JOIN item ON ( item_id = id ) 
SET checked = 0 
WHERE ancestor_id = 1

UPDATE closure, item 
SET checked = 0 
WHERE ancestor_id = 1 AND item_id = id

两者都适用于MySQL,但那些在SQLite中给我一个语法错误。

如何使这个UPDATE / JOIN与SQLite版本3.5.9一起使用?

2 个答案:

答案 0 :(得分:123)

你做不到。 SQLite doesn't support JOINs in UPDATE statements

但是,您可以使用子查询来执行此操作:

UPDATE closure SET checked = 0 
WHERE item_id IN (SELECT id FROM item WHERE ancestor_id = 1);

或类似的东西;目前还不清楚你的架构到底是什么。

答案 1 :(得分:6)

您也可以使用REPLACE,然后可以使用连接选择。 像这样:

REPLACE INTO clusure 
 SELECT sel.col1,sel.col2,....,sel.checked --checked should correspond to column that you want to change
FROM (
 SELECT *,0 as checked FROM closure LEFT JOIN item ON (item_id = id) 
 WHERE ancestor_id = 1) sel