【问题标题】:SQLite: updating column from another tableSQLite:从另一个表更新列
【发布时间】:2018-05-04 14:05:50
【问题描述】:

我有一张旧桌子和一张新桌子。我需要的是将旧表的 uuId 复制到新表中。

我正在关注其他参考资料的一些答案,但我无法得到理想的答案。

我找到的最接近的答案是:

update table1
set table1.uuid = 
(select table2.uuid from table2 where table1.itemDescription = table2.itemDescription)

当我执行此查询时,它只会将旧表的第一个找到的 uuid 保存到新表中的所有条目中。

Sample Table2(旧表):

uuid|itemDescription
   1|item1
   2|item2
   3|item3

示例表1(新表):

uuid|itemDescription
Null|item1
Null|item2
Null|item3

期望的输出:

uuid|itemDescription
   1|item1
   2|item2
   3|item3

会发生什么:

uuid|itemDescription
   1|item1
   1|item2
   1|item3

【问题讨论】:

  • 您的UPDATEtable2.uuid 复制到table1.uuid,但是,您的样本数据在table2.uuid 中没有任何值。此外,itemDescription 在两个表中都不对应。
  • 美好的一天,对不起,我编辑它是正确的。表 2 应该是第一个样本。有身份证的。并且新表应该是没有 id 的表。

标签: sqlite


【解决方案1】:

在 SQLite 中,不能在 SET 子句中使用表名:

update table1
set uuid = 
(select table2.uuid from table2 where table1.itemDescription = table2.itemDescription);

【讨论】:

  • 非常感谢我昨天也刚刚找到它,我的查询有什么问题,但你已经发布了,所以我接受了。非常感谢。
【解决方案2】:

试试这样:

UPDATE table1
SET table1.uuid = table2.uuid
FROM table2 WHERE table1.itemDescription = table2.itemDescription

不需要子查询,否则你还需要在外部查询中链接项目描述

【讨论】:

  • 在 From 附近给我一个错误。我正在使用 SQLite。而且我不久前已经得到了这个答案T.T
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
相关资源
最近更新 更多