mysql联表更新(不同于sql),mssql 跨服务器联表更新

1.更新单个字段(子查询)

UPDATE 表A set 字段A = (select 字段B from  表B where B.外键 = A.外键);

# eg: 
UPDATE A计划库 set 部门id = (select id from  sys_d_department where dept_name = 部门);

mssql更新语句为:

update 表A set [字段A] = B.Id from 表B B where B.TypeName = BridgeLevel;

2.更新字段1和字段2(LEFT JOIN)

UPDATE table_b AS b 
LEFT JOIN table_a AS a 
ON b.id= a.id 
SET b.field_1 = a.field_1 , b.field_2 = a.field_2 ;

# eg:
update BMS_D_PavIndex set SegCode = B.SegmentCode from  BMS_D_PavIndex A 
left join RMS_D_Segment B on  A.RouteCode =  B.RouteCode and A.BeginMile = B.BeginMile 
where  B.SegmentCode like '%A%'
;

运用concat()实现模糊查询

mssql更新语句为:
update 表A set 字段A = 字段B from 表A left join 表B on 表A.外键 = 表B.外键 and A.name like concat(B.name, '%');

3.通过 INNER JOIN,可以更新关联的多张表

update1  
inner join2   
on1.列名 =2.列名   
set1.列名 =; 

4.通过where更新(可以更新多张表)

update1,2 
set1.列名 =2.列名 
where1.列名 =2.列名 

5.mssql 跨服务器联表更新

【工具】:SQL Server 2014
新建链接服务器后,可通过[服务器名].[数据库名].[表名]的方式对表进行操作。
在这里插入图片描述

update [数据库A].[表A] set TypeId = B.typeId 
from  [数据库A].[表A] A 
inner join (
	select t_type.Id as typeId, t_asset.Id as Id from [服务器B].[数据库A].[表A] t_asset 
	left join [服务器B].[数据库A].[表B] t_bridge
	on t_asset.Id = t_bridge.AssetId
	left join BMS_S_AssetType t_type 
	on t_type.TypeName = t_bridge.BridgeLevel 
	where  t_asset.Id in (select AssetId from [服务器B].[数据库A].[表B]) ) B 
on  A.SourceId =  B.Id where  A.SourceData = '240' ;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值