简体   繁体   English

MySQL:将整行从一个复制到另一个并删除原始行

[英]MySQL: Copy entire row from one to another and delete original

Could someone please explain (or point in right direction) how I would move multiple rows from one table to another and remove the row from the original table based on a set criteria? 有人可以解释(或指向正确的方向)如何将多行从一个表移动到另一个表并根据设定的标准从原始表中删除该行?

I understand 我明白

INSERT INTO table2 SELECT * FROM table1

to copy the data from one table to another but I need to then remove the original. 将数据从一个表复制到另一个表但我需要删除原始表。 The reason being it has been suggested to speed up the querying of the table I should move all redundant data (ended, expired, products older than 3 months) from the main table to another one. 原因是有人建议加快查询表,我应该将所有冗余数据(已结束,过期,超过3个月的产品)从主表移动到另一个表。

A bit of background, I have a table that holds products, some products have expired but the products still need to be accessible. 有一些背景知识,我有一个包含产品的表,有些产品已经过期但产品仍然需要访问。 There are about 50,000 products that have expired and 2,000 which are active. 大约有50,000种产品已经过期,2,000种产品已经过期。 There is a status column (int 1 = active, 2 = expired etc) to determine what to show on the front end. 有一个状态列(int 1 = active,2 = expired等)来确定在前端显示的内容。

I guess this post is 2 questions: 我想这篇文章是2个问题:

  1. Is there a better way to speed up the querying of the product table without removing expired items? 有没有更好的方法来加速查询产品表而不删除过期的项目?
  2. If not, how to move rows from one table to another 如果没有,如何将行从一个表移动到另一个表

Many many thanks! 非常感谢!

INSERT INTO table2 (column_name1, column_name2) SELECT column_name1, column_name2 FROM table 1 WHERE (where clause here) INSERT INTO table2(column_name1,column_name2)SELECT column_name1,column_name2 FROM table 1 WHERE(where子句)

DELETE FROM table1 WHERE (where clause here) DELETE FROM table1 WHERE(where子句)

Source for above: mysql move row between tables 上面的来源: mysql在表之间移动行

50,000 records in the table really isn't that many. 表中的50,000条记录真的不是那么多。 If you're having performance issues, I'd look at your queries and your indexes to help speed up performance. 如果您遇到性能问题,我会查看您的查询和索引,以帮助加快性能。 And since those expired records still need to be accessed, then it could be more difficult having multiple tables to maintain. 由于仍然需要访问那些过期的记录,因此维护多个表可能会更加困难。

However, to move data from one table to another as you've asked, you just need to run 2 different statements. 但是,要按照您的要求将数据从一个表移动到另一个表,您只需运行2个不同的语句。 Assuming you want to move inactive products: 假设您要移动非活动产品:

INSERT INTO ProductsBackup SELECT * FROM Products WHERE Status <> 1
DELETE FROM Products WHERE WHERE Status <> 1

If you have Identities on your columns, you might be better off specifying the column names. 如果列上有Identities,则最好指定列名。 But assuming the ProductId is the Identity, then be careful moving those to a different table as you probably don't want to lose that original id as it may point to other tables. 但假设ProductId是Identity,那么请小心将它们移到另一个表中,因为您可能不希望丢失原始ID,因为它可能指向其他表。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM