--tg_couponinfo表名 tg_order表名
我想同时更新tg_couponinfo tg_order两个表中smallimg字段 根据proudctid id关联;下面SQL报错!我该怎么写可以实现?
update tg_couponinfo set smallimg='05710010/sl20101227151802.jpg' and a.smallimg='05710010/sl20101227151802.jpg' inner join tg_order as c on b.productID=c.productID inner join tg_product as a on a.id=b.productID;
我想同时更新tg_couponinfo tg_order两个表中smallimg字段 根据proudctid id关联;下面SQL报错!我该怎么写可以实现?
update tg_couponinfo set smallimg='05710010/sl20101227151802.jpg' and a.smallimg='05710010/sl20101227151802.jpg' inner join tg_order as c on b.productID=c.productID inner join tg_product as a on a.id=b.productID;
如果是一个表里的多个字段可以放一个update里,多个表的好像不可以吧!
如果有的话,等楼下解答!
建立主外键关系,然后级联更新设置上吧。
不过,即便是级联更新,也是一个表,一个表的更新。 update还不能指向两个表。
--下面是示例 --示例数据
create table ta(关联字段 int,字段1 varchar(10))
insert ta select 1, 'aa '
union all select 2, 'bb ' create table tb(关联字段 int,字段2 varchar(10))
insert tb select 1, 'AA '
union all select 2, 'BB '
go --创建视图
create view v_ta_tb
as
select a.*,b.字段2 from ta a,tb b where a.关联字段=b.关联字段
go --创建视图的更新触发器
create trigger tr_update on v_ta_tb
instead of update
as
--更新表a
update a set 字段1=i.字段1
from ta a,inserted i
where a.关联字段=i.关联字段 --更新表b
update b set 字段2=i.字段2
from tb b,inserted i
where b.关联字段=i.关联字段
go --更新
update v_ta_tb set 字段1= 'XX ',字段2= 'YY '
go --显示更新结果
select * from ta
select * from tb
go --删除测试
drop table ta,tb
drop view v_ta_tb /*--测试结果 关联字段 字段1
----------- ----------
1 XX
2 XX (所影响的行数为 2 行) 关联字段 字段2
----------- ----------
1 YY
2 YY (所影响的行数为 2 行)
--*/
UPDATE b
SET smallimg='05710010/sl20101227151802.jpg'
FROM tg_product AS a
INNER JOIN tg_couponinfo AS b ON a.ID=b.productIDUPDATE b
SET smallimg='05710010/sl20101227151802.jpg'
FROM tg_product AS a
INNER JOIN tg_order AS b ON a.ID=b.productID
go
有其它條件時,在后面加上where 如UPDATE a
SET smallimg='05710010/sl20101227151802.jpg'
FROM tg_product AS a
INNER JOIN tg_order AS b ON a.ID=b.productID
WHERE a.NAME='產品0001'