日期:2014-05-19  浏览次数:20556 次

刚才没有一个对的!可能我说清楚!我再说一边
我insuredaccount表有四个字段
snjz               snye           bnye                 id_year     l_insured_id
542.14             0               363.5               2006           1
                                                                2007           1

其中l_insured_id   是主键

snjz     的2007年的值要是2006年bnye加snye
这个update怎么写
注意
数据库里有多条记录不是一条!
2007年为已知的

刚才都是说子查询返回的值多于一个。当子查询跟随在   =、!=、 <、 <=、> 、> =   之后,或子查询用作表达式时,这种情况是不允许的
下面的就是这样的错误
update   insuredaccount   set   snjz=(select   snye+bnye   from   insuredaccount   where   id_year=2006)
where   id_year=2007

------解决方案--------------------
--try

update insuredaccount set snjz=(select sum(snye+bnye) from insuredaccount where id_year=2006)
where id_year=2007

------解决方案--------------------
select snye+bnye from insuredaccount where id_year=2006

----------------------------------
错误就是说这个查询结果不唯一
------解决方案--------------------
update a
set
snjz=b.snye+b.bnye
from
insuredaccount a,
insuredaccount b
where
a.id_year=2007 and b.id_year=2006
------解决方案--------------------
-----test

create table insuredaccount
(snjz money,
snye money,
bnye money,
id_year char(4),
l_insured_id int)


insert into insuredaccount
select 542.14,0, 363.5, 2006, 1
union
select null,null ,null , 2007, 1


select * from insuredaccount

update insuredaccount set snjz=(select snye+bnye from insuredaccount where id_year=2006)
where id_year=2007

-------------result

snjz snye bnye id_year l_insured_id
363.5000 2007 1
542.1400 .0000 363.5000 2006 1


================
这个应该就是楼主希望的结果,在本表只有两条数据的话,没有问题
因为id_year=2007的数据只有一条
但是如果id_year=2007的数据不止一条,
LZ的句子才会出错



------解决方案--------------------
update insuredaccount set snjz=(select sum(snye+bnye) total from insuredaccount where id_year=2006) where id_year=2007
试试这个?