简体   繁体   English

MySQL选择多个记录

[英]Mysql select multiple records

I have 3 tables abc ab and c all are related through an id a.id = b.id = c.id 我有3个表abc ab和c都通过一个id相关联a.id = b.id = c.id

My task is to select all rows from ab and c where a.date is older than 6 months and insert them into a new db for archiving. 我的任务是从a.date大于6个月的ab和c中选择所有行,并将它们插入新的数据库中进行归档。

Here is what I have so far: 这是我到目前为止的内容:

insert into dbArchive.a select * from db.a where receivedDate < CURRENT_DATE() - INTERVAL 6 MONTH;`

This seems to select all entries from table a that are older than 6 months and insert them into the archive db table a. 这似乎从表a中选择了6个月以上的所有条目,并将它们插入到存档db表a中。

What is the best and most efficient way to find all rows from tables b and c that have the same id and insert them to the archive db tables b and c? 从表b和c中查找具有相同ID的所有行并将其插入到存档db表b和c中的最佳和最有效的方法是什么?

I have over 1 million records that are older than 6 months so I am weary about performance issues. 我有超过100万条超过6个月的记录,因此我对性能问题感到厌倦。

After inserting into dbArchive.a as you have done here, you can run the following to insert into dbArchive.b and same way for dbArchive.c . 像在此处完成的那样插入dbArchive.a之后,可以运行以下命令插入dbArchive.b并以与dbArchive.b相同的方式dbArchive.c

INSERT INTO dbArchive.b(id, etc..) 
  SELECT db.b.id, etc.. 
  FROM dbArchive.a INNER JOIN db.b ON dbArchive.a.id = db.b.id;

So after playing around for a while this seems to be the best approach I have found that actually works: 所以在玩了一段时间之后,这似乎是最好的方法,我发现它实际上是可行的:

INSERT INTO archive.b SELECT * FROM db.b WHERE `id` IN( SELECT `id` FROM archive.a )

I am not sure if this is the most efficient method but it gets the job done 我不确定这是否是最有效的方法,但是可以完成工作

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

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