这个SQL写法呀

ma_yan_jun_78 2003-12-23 10:43:17
create table t_1 (a1 varchar(20),b1 varchar(20),c1 varchar(20))

create table t_2 (x varchar(20),y varchar(20))

insert into t_2
select 2,2222 union
select 3,3333 union
select 4,4444

insert t_1
select 1,14,114 union
select 1,15,115 union
select 2,25,225 union
select 2,24,224 union
select 3,33,333 union
select 4,44,444 union
select 5,55,555

我想根据t_2 把t_1里的a1 在t_2里有的数据换成t_2 里的Y

也就是:(结果集如下)
1 14 114
1 15 115
2222 24 224
2222 25 225
3333 33 333
4444 44 444
5 55 555

不用游标能成吗?
...全文
42 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
gmlxf 2003-12-23
  • 打赏
  • 举报
回复
update a set a1=isNull((select y from t_2 where x=a.a1),a1) from t_1 a
shuiniu 2003-12-23
  • 打赏
  • 举报
回复
update a
set a.a1 = b.y
from t_1 a , t_2 b
where a.a1 = b.x
ma_yan_jun_78 2003-12-23
  • 打赏
  • 举报
回复
不用join 的写法有吗?大哥们!
shuiniu 2003-12-23
  • 打赏
  • 举报
回复
update a
set a.a1 = b.y
from t_1 a join t_2 b
on a.a1 = b.x

select * from t_1

/*
a1 b1 c1
-------------------- -------------------- --------------------
1 14 114
1 15 115
2222 24 224
2222 25 225
3333 33 333
4444 44 444
5 55 555

(所影响的行数为 7 行)
*/
ma_yan_jun_78 2003-12-23
  • 打赏
  • 举报
回复
up 下

34,838

社区成员

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

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