SQL如何对不同表的数据进行更新
2023-06-13 09:15:29 时间
如果我们有表A和表B, 我想把我的表A的Col1内的数据更新到表B的Col1里面,那么我们怎么做呢?
Microsoft SQL例子
UPDATE scores
SET scores.name = p.name
FROM scores s
INNER JOIN people p
ON s.personId = p.id
MySQL例子
UPDATE scores s,
people p
SET scores.name = people.name
WHERE s.personId = p.id
高阶使用
当我们从一个上传的表,与多个表合并查询后,再更新,就会花费很多时间。那么,有没有办法一次性,将上传的表与需要的数据合并后再根据条件更新呢?
MERGE SQL使用
The MERGE statement is used to manipulate (INSERT, UPDATE, DELETE) a target table by referencing a source table for the matched and unmatched rows. The MERGE statement can be very useful for synchronizing the table from any source table.
Now, if we go back to our position, the MERGE statement can be used as an alternative method for updating data in a table with those in another table. In this method, the reference table can be thought of as a source table and the target table will be the table to be updated. The following query can be an example of this usage method.
MERGE Persons AS Per
USING(SELECT * FROM AddressList) AS Addr
ON Addr.PersonID=Per.PersonID
WHEN MATCHED THEN
UPDATE SET
Per.PersonPostCode=Addr.PostCode ,
Per.PersonCityName = Addr.City;
相关文章
- SQL开发知识:SQL Server实现自动循环归档分区数据脚本
- MySQL如何导入SQL文件(mysql怎么导入sql文件)
- Oracle事务SQL:处理复杂数据问题(oracle事务sql)
- SQL与MSSQL:各有自身特性的不同之处(sql与mssql的区别)
- 如何使用MySQL定时自动执行SQL语句(mysql定时执行sql)
- 排序以SQL Server实现数据逆序排序(sqlserver 逆序)
- 使用SQL Server实现纵向数据联接(sqlserver纵连接)
- 如何使用 Oracle SQL 美化数据查询结果?(oraclesql美化)
- :『如何使用SQL Server轻松收藏数据?』(sqlserver 收藏)
- SQL Server 提交行实现数据更新(sqlserver提交行)
- 除数据SQL Server如何删除数据(sqlserver咋删)
- 利用SQL Server实现关联表间数据交互(sqlserver关联表)
- Oracle数据库如何导出SQL文件?(oracle 导sql)
- SQL Server数据库修复:解决数据损坏问题(sqlserver修复’)
- 数据SQL Server实现数据表持久化保存(sqlserver保存表)
- 架构SQL Server下的三层架构:开发更高效的数据应用(sqlserver 三层)
- Oracle中使用SQL实现取整(oracle中sql取整)
- Oracle SQL数据解析的利器(oracle sql计算)
- Oracle SQL让你的数据不再缺位(oracle sql补位)
- Oracle SQL 实现数据传值(oracle sql传值)