简体   繁体   English

MySQL 查询删除两个数据库 RDS(MYSQL DB)中常见的数据

[英]MySQL QUERY TO DROP DATA WHICH ARE COMMON IN TWO DATABASES RDS (MYSQL DB'S)

eg:例如:

         **table 1**

 name     gender  email

 hemanth    m       test@gmail.com 
 rajesh     m       rajesh@gmail.com 
 kumar      m       kumar@gmail.com

      **table 2***

 name     gender    email

 hemanthsai m       testing@gmail.com 
 rajesh     m       rajesh@gmail.com 
 kumaras    f       kumaras@gmail.com

     **expected result in table 1***

 name     gender    email

 hemanthsai m       testing@gmail.com 
 rajesh     m       rajesh@gmail.com 
 kumaras    f       kumaras@gmail.com

this is just an example, in reality i have to update all the data with many columns at a time in mysql这只是一个例子,实际上我必须在 mysql 中一次更新所有包含许多列的数据

What I've tried was我试过的是

     INSERT INTO table1 (NAME,gender, email) VALUES(?, ?, ?), (?, ?, ?)
     ON DUPLICATE KEY UPDATE SET *;    ### this didn""t worked ###

I need something like:我需要类似的东西:

     INSERT INTO table1 * ON DUPLICATE KEY UPDATE SET *; 

If this didn't work then I thought of delete common values on one table using another table and then insert the left values into the deleted table, so I need query to delete common values on one table based on another table..如果这不起作用,那么我想到了使用另一个表删除一个表上的公共值,然后将左侧值插入到已删除的表中,所以我需要查询以根据另一个表删除一个表上的公共值..

thanks in advance,提前致谢,

Have you tried INSERT INTO SELECT ?您是否尝试过INSERT INTO SELECT

Example:例子:

INSERT INTO `table1`
(`name`, `gender`, `email`)
SELECT t2.`name`, t2.`gender`, t2.`email`
FROM `table2` AS t2
ON DUPLICATE KEY UPDATE
`name`= t2.`name`,`gender` = t2.`gender`, `email` = t2.`email`

You also need to have a unique key which is common between table1 and table2.您还需要有一个唯一的键,它在 table1 和 table2 之间是通用的。 I assume that is the case.我认为情况就是这样。

EDIT编辑

Taking your comment into account, what you are looking for might be REPLACE INTO .考虑到您的评论,您正在寻找的可能是REPLACE INTO

REPLACE INTO `table1` 
SELECT *
FROM `table2`

Please note that this operation doesn't update the old values, but actually deletes them.请注意,此操作不会更新旧值,而是实际删除它们。 This means that the PRIMARY_KEY might be replaced if it has for instance auto_increment() .这意味着如果PRIMARY_KEY具有例如auto_increment() ,它可能会被替换。 Also note that this is a heavy operation as the table needs to be reindexed for every delete operation.另请注意,这是一项繁重的操作,因为每次删除操作都需要重新索引表。

You can read more about it here: http://code.openark.org/blog/mysql/replace-into-think-twice你可以在这里阅读更多信息: http://code.openark.org/blog/mysql/replace-into-think-twice

Personally I would use INSERT INTO even if there are many columns即使有很多列,我个人也会使用INSERT INTO

Create another table:创建另一个表:

CREATE TABLE table3 (
  name VARCHAR(50),
  gender VARCHAR(50),
  email VARCHAR(50));

Create a unique index on the new table:在新表上创建唯一索引:

CREATE UNIQUE INDEX index_name
ON table3 (name, gender, email);

Do INSERT IGNORE from both of table1 & table2:从 table1 和 table2 执行INSERT IGNORE

INSERT IGNORE INTO table3 SELECT * FROM table1;

INSERT IGNORE INTO table3 SELECT * FROM table2;

Check your table3 , it should't have any duplicates anymore.检查您的table3 ,它应该不再有任何重复项。

Demo演示

P/S: If you want to only check duplicate email - and allow same user having multiple emails, you can skip creating unique index and make email column in table3 as PRIMARY KEY instead. P/S:如果您只想检查重复的 email - 并允许同一用户拥有多个电子邮件,您可以跳过创建唯一索引并将table3中的email列作为PRIMARY KEY

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

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