将列数据的子集从一个表复制到另一个表
我有两个具有相同架构的表。我们将它们命名为 TestTable 和 TestTableTemp。我只需将两列从 TestTableTemp 复制到 TestTable,而不会破坏其他数据。 TestTable 中的行是 TestTableTemp 中行的子集。假设我需要复制的列名为 Column1 和 Column2,并且它们具有由列 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的原始比较查询:
这是工作查询(我只是稍微更改了您的版本):
http:// /sqlfiddle.com/#!5/f3a19/9
Your original query for comparison:
Here is the working query (I just slightly changed your version):
http://sqlfiddle.com/#!5/f3a19/9