日期:2014-05-20  浏览次数:20668 次

2个查询 难度

a表
id1,id2,s
id1,id2     2个是主键

b表
id1,id2,s1
id1,id2     2个是主键

要求写一个SQL语句删除a表中id1,id2和b表中id1,id2相同的记录!


a表
id1,s1


b表
id1,s2

要求对a表id1字段=b表id1时候,将a表s1设置为b表根据id1分组sum(s2)值,
即select   id1,sum(s2)   from   b   group   by   id1




------解决方案--------------------
up

------解决方案--------------------
delete from a
where exists (select 'Z ' from b where a.id1=b.id1 and a.id2=b.id2)

update a
set s1=(select sum(s2) from b where a.id1=b.id1 group by b.id1)
------解决方案--------------------
delete a
from a
where exists(select 1 from b where b.id1 = a.id1 and b.id2 = a.id2


update c
set s1 = temp.curSum
from c, (select id1, sum(s2) as curSum from d group by id1)temp
where c.id1 = temp.id1