[英]Copy Row into another Table with an Update
Ok, the title may be difficult to understand, but this is what I want to do. 好的,标题可能很难理解,但这是我想要做的。
I have a Table 'A' with a few columns. 我有一个带有几列的表“ A”。 I have a Table 'B' with the exact same columns.
我有一个表'B',具有完全相同的列。
During my program flow, I insert a few rows from 'A' into 'B' (sort of a temp cache). 在程序流程中,我将“ A”中的几行插入“ B”中(一种临时缓存)。 Column data in 'A' may change, but when someone clicks the 'restore' button, I wanna copy those rows from 'B' back into 'A' and overwrite those rows with the same id as the ones i want to copy from 'B'.
“ A”中的列数据可能会更改,但是当有人单击“恢复”按钮时,我想将“ B”中的那些行复制回“ A”中,并用与我要从“”中复制的行相同的ID覆盖那些行。 B'。
Eg 例如
Table A:
1 : Hi |
2 : Friends |
3 : What's up
During the flow i backup id 1 and 2 在流程中,我备份了ID 1和2
Table B:
1 : Hi |
2 : Friends
Now I do some stuff and table A changes a bit 现在我做了一些事情,表A发生了一些变化
Table A (changed):
1 : Hi diddely doo |
2 : Amigo's |
3 : What's up
Now I realise my changes are wrong, and I want to put the records with id 1 and 2 back in their original state, from table 'B'. 现在,我意识到我的更改是错误的,我想将ID为1和2的记录放回表'B'的原始状态。
Is there a 'simple' sql to do that, without fetching those id's 1 by 1 and update it 1 by 1? 是否有一个“简单”的SQL来做到这一点,而没有以1的方式获取那些ID并以1的方式对其进行更新?
Thanks in advance! 提前致谢!
You can use JOIN in UPDATE statements : 您可以在UPDATE语句中使用JOIN :
UPDATE ta SET ta.text = tb.text
INNER JOIN tb ON ta.id = tb.id
Or with implicit join: 或使用隐式联接:
UPDATE ta, tb SET ta.text = tb.text
WHERE ta.id = tb.id
You can delete and reinsert, that way you don't have to specify columns if both tables have the same table scheme: 您可以删除并重新插入,这样,如果两个表具有相同的表方案,则不必指定列:
DELETE FROM ta WHERE id IN (SELECT id FROM tb)
INSERT INTO ta
SELECT * FROM tb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.