我正在尝试从以下位置移动旧数据:
this_table >> this_table_archive
正在复制所有列。我试过了,但不起作用:
INSERT INTO this_table_archive (*) VALUES (SELECT * FROM this_table WHERE entry_date < '2011-01-01 00:00:00');
注意:这两个表是相同的,并且将id
设置为主键。
发布于 2011-03-10 06:56:43
manual中描述了正确的语法。试试这个:
INSERT INTO this_table_archive (col1, col2, ..., coln)
SELECT col1, col2, ..., coln
FROM this_table
WHERE entry_date < '2011-01-01 00:00:00';
如果id列是一个自动增量列,并且在两个表中都已经有一些数据,那么在某些情况下,您可能希望从列列表中省略id并生成新的id,以避免插入原始表中已经存在的id。如果你的目标表是空的,那么这不是问题。
发布于 2011-03-10 06:57:49
对于语法,它看起来像这样(省略列列表以隐含地表示"all")
INSERT INTO this_table_archive
SELECT *
FROM this_table
WHERE entry_date < '2011-01-01 00:00:00'
用于避免在存档表中已有数据时出现主键错误
INSERT INTO this_table_archive
SELECT t.*
FROM this_table t
LEFT JOIN this_table_archive a on a.id=t.id
WHERE t.entry_date < '2011-01-01 00:00:00'
AND a.id is null # does not yet exist in archive
发布于 2011-03-10 06:59:26
你不需要对值位使用double ()吗?如果没有,试试这个(尽管肯定有更好的方法
insert into this_table_archive (id, field_1, field_2, field_3)
values
((select id from this_table where entry_date < '2001-01-01'),
((select field_1 from this_table where entry_date < '2001-01-01'),
((select field_2 from this_table where entry_date < '2001-01-01'),
((select field_3 from this_table where entry_date < '2001-01-01'));
https://stackoverflow.com/questions/5253302
复制相似问题