Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何比较ID在表2中插入新行到表1中?_Mysql_Sql - Fatal编程技术网

Mysql 如何比较ID在表2中插入新行到表1中?

Mysql 如何比较ID在表2中插入新行到表1中?,mysql,sql,Mysql,Sql,我想知道如何在表2的表1中插入新行。其思想是,通过比较两个表,如果在第二个表中,您在表1中找不到相同的ID,则会在表1中插入新数据 这是两张桌子和我想做的想法: Tabla 1 ID-Name-Surname 1-Oriol-Molina Tabla 2 ID-Name-Surname 1-Oriol-Molina 2-Ricard-Martin 结果是: Tabla 1 ID-Name-Surname 1-Oriol-Molina 2-Ricard-Martin

我想知道如何在表2的表1中插入新行。其思想是,通过比较两个表,如果在第二个表中,您在表1中找不到相同的ID,则会在表1中插入新数据

这是两张桌子和我想做的想法:

Tabla 1  
ID-Name-Surname  
1-Oriol-Molina

Tabla 2
ID-Name-Surname  
1-Oriol-Molina  
2-Ricard-Martin
结果是:

Tabla 1  
ID-Name-Surname  
1-Oriol-Molina  
2-Ricard-Martin

Tabla 2
ID-Name-Surname  
1-Oriol-Molina  
2-Ricard-Martin
您可以使用with
LEFT JOIN
IS NULL
检查,仅从
Table2
中提取那些在
Table1
中不存在的行

INSERT INTO Table1 (ID, Name, Surname)
SELECT t2.ID, t2.Name, t2.Surname 
FROM Table2 t2 
LEFT JOIN Table1 t1 ON t1.ID = t2.ID 
WHERE t1.ID IS NULL
您可以使用with
LEFT JOIN
IS NULL
检查,仅从
Table2
中提取那些在
Table1
中不存在的行

INSERT INTO Table1 (ID, Name, Surname)
SELECT t2.ID, t2.Name, t2.Surname 
FROM Table2 t2 
LEFT JOIN Table1 t1 ON t1.ID = t2.ID 
WHERE t1.ID IS NULL

您可以尝试使用左连接

insert into table1 
  select id, name, surname from table2 left join table1 on table2.id=table1.id
  where table1.id is null

您可以尝试使用左连接

insert into table1 
  select id, name, surname from table2 left join table1 on table2.id=table1.id
  where table1.id is null

使用数据库强制执行数据完整性。也就是说,如果不希望表中存在重复的
id
s,请声明唯一索引/约束:

create unique index unq_table1_id on table1(id);
然后,在MySQL中,您可以对重复密钥更新使用

insert into table1 (id, name, surname)
    select id, name, surname
    from table2 
    on duplicate key update id = values(id);
最后一个语句是no-op——它除了防止错误之外什么都不做


这种方法的优点是,数据库将确保
id
对于将数据插入表中的任何语句都是唯一的,而不仅仅是此语句。

使用数据库强制执行数据完整性。也就是说,如果不希望表中存在重复的
id
s,请声明唯一索引/约束:

create unique index unq_table1_id on table1(id);
然后,在MySQL中,您可以对重复密钥更新使用

insert into table1 (id, name, surname)
    select id, name, surname
    from table2 
    on duplicate key update id = values(id);
最后一个语句是no-op——它除了防止错误之外什么都不做

这种方法的优点是,数据库将确保
id
对于任何向表中插入数据的语句都是唯一的,而不仅仅是这个语句