查询一张表的结果,在另一张表中做添加或修改
查询一张表的结果,在另一张表中没有name做添加有做修改操作!
A表
name fraction status
张三 80 0
李四 70 0
王五 58 1
B表
name fraction
张三 20
王五 58
要个结果是
B表
name fraction
张三 80
李四 70
王五 58
[解决办法]
--更新update bset b.fraction = a.fractionfrom a join b on a.name = b.name--插入insert into bselect name,fractionfrom a twhere not exists (select 1 from b where name = t.name)
[解决办法]
加个存储过程的创建,把那段放进去就是了。
create proc get_UpdateAndInsertasbegin--更新update bset b.fraction = a.fractionfrom a join b on a.name = b.name--插入insert into bselect name,fractionfrom a twhere not exists (select 1 from b where name = t.name)end
[解决办法]
create table a(name varchar(10),fraction int,status bit)create table b(name varchar(10),fraction int)insert aselect '张三',80,0 union allselect '李四',70,0 union allselect '王五',58,1insert bselect '张三',20 union allselect '王五',58goinsert b(name,fraction) select a.name,a.fraction from a where not exists(select 1 from b where a.name=b.name)/*(所影响的行数为 1 行)*/goselect * from b/*name fraction---- -----张三 20王五 58李四 70*/godrop table a,b