如何去另一个表取出数据更新到当前表

SQL2000我有表tb1,tb2,tb3
tb1的字段product_no,color,product_size,orderno,
tb2字段joinno,orderno,product_info
tb3字段,product_no,product_type

那么我想更新表tb2的字段product_info是根据orderno字段去查询tb1表的product_no,color,product_size,再根据tb1的product_no去查询tb3的product_type,

最后后成product_info字段内容为product_no-color-product_size-product_type

tb1表
product_no | color | product_size | orderno

SSA1Sf2219 | 黑色 | 39 | SS2001011052201

tb2表

joinno | orderno | product_info
dddd |SS2001011052201 | SSA1Sf2219-黑色-39-男鞋

tb3表
product_no | product_type

SSA1Sf2219 | 男鞋

如下命令都可以实现

 

update tb2 set product_info = tb1.product_no+'-'+tb1.color+'-'+tb1.product_size+'-'+tb3.product_type
       
from tb1,tb3
       
where tb1.orderno = tb2.orderno
       
and tb1.product_no = tb3.product_no

 

 

 

update tb2 set product_info=(
select b.product_no +'-'+ b.color +'-'+ c.product_type
from tb1 b
inner join tb3 c on c.product_no=b.product_no
where a.orderno=b.orderno
)
from tb2 a

 

 

UPDATE  tbl_protect_log
SET         product_no2 = tbl_order.product_no + '-' + tbl_order.color + '-' + tbl_order.product_size + '-' + tbl_product_store.product_type
FROM      tbl_order INNER JOIN
                tbl_protect_log ON tbl_order.orderno = tbl_protect_log.orderno INNER JOIN
                tbl_product_store ON tbl_order.product_no = tbl_product_store.product_no

 

 

 

 

原文地址:https://www.cnblogs.com/xiaofengfeng/p/1886046.html