我试图只更新表中的某些行,但其他行得到 NULL

发布于 2024-12-19 11:23:54 字数 210 浏览 3 评论 0原文

我正在尝试对 sqlite 数据库中的一组特定行运行简单的更新查询。它可以工作,但它会清空所有其他行。这是查询:

update table1 set col5 =(select col5 from table2 where table2.id = table1.id)

我知道这非常简单,但我不知道发生了什么。我不能只更新某些行,而不管其余行吗?

I am trying to run a simple Update query on a certain set of rows in my sqlite db. It works, but it NULLs out all the other rows. Here is the query:

update table1 set col5 =(select col5 from table2 where table2.id = table1.id)

I know this is super easy, but I can't figure out what is going on. Can't I only update certain rows, and leave the rest alone?

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

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

发布评论

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

评论(2

王权女流氓 2024-12-26 11:23:54

试试这个:

update table1 inner join table2 on table1.id = table2.id
set table1.col5 = table2.col5

编辑:

抱歉,没有意识到它是 sqlite。 sqlite 不支持更新子句中的联接。
我最好的猜测是您可以执行以下操作:

update table1 set col5 = (select col5 from table2 where table2.id = table1.id)
where id IN (SELECT id FROM table2)

try this:

update table1 inner join table2 on table1.id = table2.id
set table1.col5 = table2.col5

Edit:

Sorry, didn't realise it was sqlite. sqlite doesn't support joins in the update clause.
My best guess is you could do the following:

update table1 set col5 = (select col5 from table2 where table2.id = table1.id)
where id IN (SELECT id FROM table2)
腹黑女流氓 2024-12-26 11:23:54

您正在使用更新的相同列来关联两个表。您应该使用主键或辅助键来关联两个表。

请参阅这个非常相似的问题及其答案: 基于 ID 匹配从一个表到另一个表的 SQL 更新

You are using the same columns you are updating to relate the two tables. You should use a primary or secondary key to relate the two tables.

See this very similar question and its answer: SQL update from one Table to another based on a ID match

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