简体   繁体   English

当两个表字段在MySQL中具有相同的值时,如何忽略将表中的记录插入到另一个表中?

[英]How to ignore inserting records from a table to another table when both table field has same value in MySQL?

I have 2 tables which have names and emails. 我有2个表,其中包含姓名和电子邮件。 Now I want to merge these tables into a new table without duplicate records. 现在我想将这些表合并到一个没有重复记录的新表中。 I want to use email fields for avoiding duplicate values in both tables. 我想使用电子邮件字段来避免两个表中的重复值。 I heard INSERT IGNORE query is using for inserting values into a table without affecting existing records. 我听说INSERT IGNORE查询用于在不影响现有记录的情况下将值插入表中。 How to write INSERT IGNORE query to check email field for duplication checking. 如何编写INSERT IGNORE查询以检查电子邮件字段以进行复制检查。 If anyone knows other methods are also welcome. 如果有人知道其他方法也欢迎。

table1:
fid fname   email
--- -----   -----
1   Balaji  balaji@email.com
2   xxxxx    xxxxx@email.com
3   Bala    bala@email.com

table2:

gid gname  gemail
--- -----  ------
1   Bala   bala@email.com
2   vinoth vinoth@email.com

Expected result: 预期结果:

table3:
-------
id name   email
-- ----   -----
1   Balaji  balaji@email.com
2   xxxxx    xxxxx@email.com
3   Bala    bala@email.com
4   vinoth vinoth@email.com

MySQL support UPDATE ON DUPLICATE KEY but in order to work, you need to add a unique constraint on the table you want to insert. MySQL支持UPDATE ON DUPLICATE KEY但为了工作,您需要在要插入的表上添加unique约束。

Assuming Table3 is the name of your new table. 假设Table3是新表的名称。 You need to add constraint first, 你需要先添加约束,

ALTER TABLE Table3 ADD CONSTRAINT tb_uq UNIQUE (name, email)

and you can now have unique records on the new table, to merge the previous table, 并且您现在可以在新表上拥有唯一记录,以合并上一个表,

INSERT INTO table3(name, email)
SELECT name, email 
FROM
(
    SELECT fid id, fname name, email FROM Table1
    UNION ALL
    SELECT gid id, gname name, gemail email FROM Table1
) s
ON DUPLICATE KEY UPDATE name = VALUES(name);

An alternative solution without using ON DUPLICATE KEY UPDATE . 不使用ON DUPLICATE KEY UPDATE的替代解决方案。 is to use UNION ( without ALL ) and assumes that Table3.ID is set as auto-increment 是使用UNION没有ALL )并假定Table3.ID设置为自动增量

INSERT INTO table3(name, email)
SELECT name, email 
FROM
(
    SELECT fname name, email FROM Table1
    UNION
    SELECT gname name, gemail email FROM Table2
) s

暂无
暂无

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

相关问题 MySQL更新字段与来自同一表中另一个字段的值 - mysql update field with value from another field in same table 将MySQL查询的结果插入表中时如何设置字段的值 - How to set the value of a field when inserting the results of a MySQL query into a table MySQL查找一个表中的记录,而该表没有另一表中的列的值 - MySQL Find records in one table that has no value of a column in another table MySQL的。 将一个表中的记录与另一个表中不具有公共字段的合并 - MySQL . Combine records from one table with another table with not common field MySQL:在不锁定的情况下将记录插入一个从另一个表读取的表中 - MySQL: Inserting records in one table reading from another, without locking MySQL:如果另一个表包含某个值,则在求和字段时忽略行 - MySQL: Ignore row when summing a field if another table contains a certain value 根据另一个表中的现有值在MySQL表中插入记录 - inserting records in a MySQL table depending on existing values in another table 从mysql表在远程数据库上插入记录 - inserting records from mysql table on remote database 达到限制后如何停止在MySQL表中插入记录 - How to stop inserting records in MySQL table when a limit is reached 如何使用MySQL将一个表的一个列(字段)复制到另一个表的空列(字段),这两个表都是同一数据库的一部分? - How can I copy one column (field) from one table to an empty column (field) of another table both of which are part of same database using MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM