[英]Need MySQL query to update/add record into table1 from table2
I want MySQL query to update/Add table1 if data exist in table2 如果表2中存在数据,我希望MySQL查询更新/添加表1
If record not exist in table1 than add record (from table2) otherwise update it. 如果记录在表1中不存在,则添加记录(来自表2),否则对其进行更新。
Thanking you in advance. 预先感谢您。
IF (EXISTS (SELECT * FROM table1 WHERE <condition>))
begin
UPDATE table1
SET <specify new value>
end
else
begin
INSERT INTO table1
SELECT <column list> from table2 where <condition>
end
You can copy all entries from table2 to table1 using the following syntax, assuming ID is the primary key of the tables: 假设ID是表的主键,则可以使用以下语法将所有条目从table2复制到table1:
INSERT INTO table1 SELECT * FROM table2 WHERE ID NOT IN (SELECT ID FROM table1);
The Update-Query is not so generic: Update-Query不是那么通用:
UPDATE table1 SET <COL>=(SELECT <COL> FROM table2 WHERE table1.ID=table2.ID);
You'll have to perform the above update query for each Column in your tables. 您必须对表中的每个列执行上述更新查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.