帮忙也个update ,把表A uid改成表B相同name下uid最大的。谢谢

hleb231 2019-08-27 10:03:49


if OBJECT_ID('tempdb..#A') is not null
drop table #A
if OBJECT_ID('tempdb..#B') is not null
drop table #B
create table #A
(
uid int
)
insert into #A
select 1 union all
select 10 union all
select 10 union all
select 1 union all
select 8 union all
select 8 union all
select 35
go
create table #B
(
uid int ,
name varchar(10)
)
insert into #B
select 1,'AA' union all
select 10,'AA' union all
select 8,'BB' union all
select 35,'BB'
go


...全文
99 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
二月十六 版主 2019-08-27
  • 打赏
  • 举报
回复
;WITH cte AS (
SELECT #A.uid,name FROM #A JOIN #B ON #B.uid = #A.uid
)
UPDATE cte SET cte.uid=t.uid FROM (SELECT name,MAX(uid) AS uid FROM #B GROUP BY name)t WHERE cte.name=t.name

SELECT * FROM #A

hleb231 2019-08-27
  • 打赏
  • 举报
回复
分太少了么,我已经6年没写过SQL。效率【face]onion:3.gif[/face]

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧