将行迁移到新表,然后删除它们

发布于 2024-11-19 00:50:28 字数 134 浏览 14 评论 0原文

我有一个大型产品数据库,每天我希望运行一个脚本,将 active = '1' 的行移动到新表中并删除原始行。

但我似乎找不到合适的 Mysql 命令来完成迁移。

如果有人能够阐明这一情况,那就太好了。

多谢

I have a large database of products and everyday I wish to run a script that moves rows with active = '1' into a new table and deletes the original row.

But I can't seem to find the appropriate Mysql command to accomplish the migration.

It would be great if someone could shed some light on the situation.

Thanks a lot

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

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

发布评论

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

评论(2

飘落散花 2024-11-26 00:50:28

您应该能够通过以下内容完成此操作

CREATE TABLE NewTable LIKE OldTable;

INSERT INTO NewTable 
SELECT * FROM OldTable WHERE Active = 1;

DELETE FROM OldTable WHERE Active = 1;

正如好奇,这样做的意义何在?如果您担心模糊行,可以按如下方式删除

DELETE OldTab FROM OlTable AS OldTab
INNER JOIN NewTable AS NewTab
ON OldTab.ID = NewTab.ID

You should be able to complete this with the following

CREATE TABLE NewTable LIKE OldTable;

INSERT INTO NewTable 
SELECT * FROM OldTable WHERE Active = 1;

DELETE FROM OldTable WHERE Active = 1;

Just as curiosity, what is the point of doing this? If you're worried about the fuzzy rows you could do the delete as follows

DELETE OldTab FROM OlTable AS OldTab
INNER JOIN NewTable AS NewTab
ON OldTab.ID = NewTab.ID
画中仙 2024-11-26 00:50:28

没有任何细节,您将执行以下操作:

创建一个 INSERT 语句到新表 AS SELECT from the old table WHERE active = 1。

创建一个 DELETE 语句,从第一个表中删除在第二个表中找到的任何行。

犯罪;

without any details, here is what you will do:

create an INSERT statement to the new table AS SELECT from the old table WHERE active = 1.

create a DELETE statement that deletes from the first table any rows it finds in the second table.

commit;

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