将列数据的子集从一个表复制到另一个表

发布于 2024-12-11 09:28:36 字数 1266 浏览 0 评论 0原文

我有两个具有相同架构的表。我们将它们命名为 TestTableTestTableTemp。我只需将两列从 TestTableTemp 复制到 TestTable,而不会破坏其他数据。 TestTable 中的行是 TestTableTemp 中行的子集。假设我需要复制的列名为 Column1Column2,并且它们具有由列 primaryKey 引用的相同主键。


在 mysql 中,我相信这可以这样做或类似的事情:

UPDATE TestTable, TestTableTemp
SET TestTable.Column1 = TestTableTemp.Column1, TestTable.Column2 = TestTableTemp.Column2
WHERE TestTable.primaryKey = TestTableTemp.primaryKey

Sqlite 不允许在更新语句上定义多个表,如在此处的参考数据中所示: http://www.sqlite.org/lang_update.html

我能想到的最好的办法是:

UPDATE TestTable SET
Column1 = (select TestTableTemp.Column1 from TestTableTemp, TestTable where TestTable.primaryKey = TestTableTemp.primaryKey),
Column2 = (select TestTableTemp.Column2 from TestTableTemp, TestTable where TestTable.primaryKey = TestTableTemp.primaryKey) 
WHERE EXISTS(select * from TestTableTemp where TestTable.primaryKey = TestTableTemp.primaryKey"

这给了我一个靠近“.”的语法错误。我猜这是因为我无法在标量表达式中引用 TestTable。

有人能指出我正确的方向吗?非常感谢任何帮助。

编辑:

我稍微清理了第二个查询。似乎只是将 Column1 和 Column2 设置为 TestTableTemp 中该列的第一行。

I have two tables with identical schema. Let's name them TestTable and TestTableTemp. I need to copy just two columns from TestTableTemp to TestTable without disrupting other data. The rows in TestTable are a subset of those in TestTableTemp. Let's say the columns that I need to copy are named Column1 and Column2 and that they have identical primary keys reference by column primaryKey.


In mysql I believe this could be done as such or something similar:

UPDATE TestTable, TestTableTemp
SET TestTable.Column1 = TestTableTemp.Column1, TestTable.Column2 = TestTableTemp.Column2
WHERE TestTable.primaryKey = TestTableTemp.primaryKey

Sqlite does not allow for multiple tables to be defined on the update statement as can been seen in their reference data here: http://www.sqlite.org/lang_update.html

The best I could come up with is such:

UPDATE TestTable SET
Column1 = (select TestTableTemp.Column1 from TestTableTemp, TestTable where TestTable.primaryKey = TestTableTemp.primaryKey),
Column2 = (select TestTableTemp.Column2 from TestTableTemp, TestTable where TestTable.primaryKey = TestTableTemp.primaryKey) 
WHERE EXISTS(select * from TestTableTemp where TestTable.primaryKey = TestTableTemp.primaryKey"

This gives me a syntax error near "." I am guessing this is because I cannot reference TestTable in the scalar expressions.

Can anyone point me in the right direction? Any help is much appreciated.

EDIT:

I cleaned up the second query a bit. It seems to just set the Column1 and Column2 to the first row from that column from TestTableTemp.

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

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

发布评论

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

评论(1

北城孤痞 2024-12-18 09:28:36

您的原始比较查询:

UPDATE TestTable, TestTableTemp
   SET TestTable.Column1 = TestTableTemp.Column1
     , TestTable.Column2 = TestTableTemp.Column2
 WHERE TestTable.primaryKey = TestTableTemp.primaryKey

这是工作查询(我只是稍微更改了您的版本):

http:// /sqlfiddle.com/#!5/f3a19/9

UPDATE TestTable
SET

  Column1 = ( SELECT TestTableTemp.Column1
              FROM   TestTableTemp
              WHERE  TestTableTemp.primaryKey = TestTable.primaryKey )

 ,Column2 = ( SELECT TestTableTemp.Column2
              FROM   TestTableTemp
              WHERE  TestTableTemp.primaryKey = TestTable.primaryKey )

WHERE EXISTS( SELECT NULL
              FROM   TestTableTemp
              WHERE  TestTableTemp.primaryKey = TestTable.primaryKey )
;

Your original query for comparison:

UPDATE TestTable, TestTableTemp
   SET TestTable.Column1 = TestTableTemp.Column1
     , TestTable.Column2 = TestTableTemp.Column2
 WHERE TestTable.primaryKey = TestTableTemp.primaryKey

Here is the working query (I just slightly changed your version):

http://sqlfiddle.com/#!5/f3a19/9

UPDATE TestTable
SET

  Column1 = ( SELECT TestTableTemp.Column1
              FROM   TestTableTemp
              WHERE  TestTableTemp.primaryKey = TestTable.primaryKey )

 ,Column2 = ( SELECT TestTableTemp.Column2
              FROM   TestTableTemp
              WHERE  TestTableTemp.primaryKey = TestTable.primaryKey )

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