请教SQL语句

lordports 2009-02-03 01:44:07
表a
table_id zip
1 2
2 3
3 5
表b
table_id zone
1 7
3 9
4 10

把表b 中zone 换成表a中zip,怎么弄?
...全文
95 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fanyeyimi 2009-02-03
  • 打赏
  • 举报
回复
很抱歉,我上面的查询语句是错误的 。子查询不能用=的

insert into b(zone) select zip from a inner join b on b.table_id=a.table_id
这是对的。
它得到的结果是:当b表中与a表的id 相同时,就会更改,否则保留原数据。
表b
table_id zone
1 2
3 5
4 10

fanyeyimi 2009-02-03
  • 打赏
  • 举报
回复
是指b表中的zone的值为zip的吗??

update b set zone=(select zip from a inner join b on b.table_id=a.table_id)

当然还有其它一些方法也是可行的。
lordports 2009-02-03
  • 打赏
  • 举报
回复
我知道怎么做了,谢谢各位
lordports 2009-02-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wufeng4552 的回复:]
引用 5 楼 lordports 的回复:
如果只生成
1 2
3 5
呢,在b表存在,a表不存在的为空呢?

你要結果是什麼?
[/Quote]
就是能不能把 4 10 去掉
只需要
1 2
3 5
水族杰纶 2009-02-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lordports 的回复:]
如果只生成
1 2
3 5
呢,在b表存在,a表不存在的为空呢?
[/Quote]
你要結果是什麼?
lordports 2009-02-03
  • 打赏
  • 举报
回复
如果只生成
1 2
3 5
呢,在b表存在,a表不存在的为空呢?
水族杰纶 2009-02-03
  • 打赏
  • 举报
回复
if object_id('a')is not null drop table a
go
create table a(table_id int, zip int)
insert a select 1, 2
insert a select 2, 3
insert a select 3, 5
if object_id('b')is not null drop table b
go
create table b( table_id int, zone int)
insert b select 1 , 7
insert b select 3 , 9
insert b select 4, 10
update b set zone=zip from b inner join a on b.table_id =a.table_id
select *from b
/*table_id zone
----------- -----------
1 2
3 5
4 10*/
百年树人 2009-02-03
  • 打赏
  • 举报
回复
update b
set zone=a.zip
from a
where a.table_id=b.table_id

这样?在b表存在,a表不存在的要不要改?
Andy__Huang 2009-02-03
  • 打赏
  • 举报
回复
update b
set zone=a.zip
from b inner join a on b.table_id=a.table_id
水族杰纶 2009-02-03
  • 打赏
  • 举报
回复
update b set zone=zip from b inner join a on b.table_id =a.table_id 

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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