例:当前有两表,p1(id,price1,price2),p2(id,price),在表p1中所有记录的price2值均为空,想price2值修改为p2表中与p1表id相同的记录的price值,其SQL语句如下:
update p1
set price2=
(select p2.price
from p1,p2
where p1.id=p2.id)
提示:子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。语句已终请高人正解?
update p1
set price2=
(select p2.price
from p1,p2
where p1.id=p2.id)
提示:子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。语句已终请高人正解?
SET price2 = B.price
FROM P1 A INNER JOIN P2 B ON A.ID = B.ID
set price2=
(select price from p2 where p1.id=p2.id)--price的值必须唯一,如果不是
需要加sum求和,max在,min小 avg平均
update p1
set price2=
min(select price from p2 where p1.id=p2.id)--
from p1,p2
where p1.id=p2.id)这里不用添加的p1表
update p1
set price2=
(select p2.price
from p2
where p1.id=p2.id)
from p1 a inner join p2 b on a.id=b.id
update p1
set price2=
(select min(price) from p2 where p1.id=p2.id)--最小
update p1
set price2=
(select max(price) from p2 where p1.id=p2.id)--最大update p1
set price2=
(select max(price) from p2 where p1.id=p2.id)--平均
楼主这样更新方式不是很好,如果p1表有的id,在p2表没有
可以加上条件:
update p1
set price2=
isnull((select min(price) from p2 where p1.id=p2.id),price2)--p2没有就不更新
update p1
set price2=
(select min(price) from p2 where p1.id=p2.id)
where --加上p1的条件