当前位置: 代码迷 >> Sql Server >> 2个表的有关问题。
  详细解决方案

2个表的有关问题。

热度:78   发布时间:2016-04-27 11:42:25.0
2个表的问题。。。。。。
2个不同表A,B有1个相同字段stu_no,A表如何用一条语句将B表中stu_no的值与A相同,(没有的才更新,有不更新。)

------解决方案--------------------
不知道B表中是否有数据,要是没有数据,好办。有数据需要有匹配规则。
------解决方案--------------------
SQL code
insert bselect a.* from aleft join b on a.stu_no=b.stu_nowhere b.stu_no is null
------解决方案--------------------
SQL code
update a set stu_no=b.stu_no from a join b on a.stu_no=b.stu_no
------解决方案--------------------
SQL code
update a set stu_no=b.stu_no from a ,b where a.stu_no=b.stu_no
------解决方案--------------------
你要更新哪个表,更新什么,具体点吧。
------解决方案--------------------
a,b 表有关联字段吗?
------解决方案--------------------
3,4楼的这个语句是啥意思啊?
update a set stu_no=b.stu_no from a ,b where a.stu_no=b.stu_no
这样写没有意义吧?!

请楼主把你意思再说明白点
------解决方案--------------------
SQL code
UPDATE a SET stu_no=b.stu_no FROM a JOIN b ON a.stu_no=b.stu_no
------解决方案--------------------
insert into 表B 
select a.* from 表A a 
where not exists(select 1 from 表B b where b.id=a.id)
  相关解决方案