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

发布于 2024-07-17 18:17:50 字数 336 浏览 7 评论 0原文

我尝试过:

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 一起使用?

I tried :

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

And:

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

Both works with MySQL, but those give me a syntax error in SQLite.

How can I make this UPDATE / JOIN works with SQLite version 3.5.9 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把昨日还给我 2024-07-24 18:17:51

你不能。 SQLite 不支持 UPDATE 语句中的 JOIN

但是,您可能可以使用子查询来做到这一点:

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

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

You can't. SQLite doesn't support JOINs in UPDATE statements.

But, you can probably do this with a subquery instead:

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

Or something like that; it's not clear exactly what your schema is.

我三岁 2024-07-24 18:17:51

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

REPLACE INTO closure 
 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

You can also use REPLACE then you can use selection with joins.
Like this:

REPLACE INTO closure 
 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文