我有一个名为STOCK
的表,我想要更新它,如下所示:
-------------------------
NUMBER | UNITS
-------------------------
NA13ALPK1010 | 9
NA13ALANA1010 | 11
NA13ALPK1065 | 4
NA13ALANA106 | 5
ON0003 XS | 1
AT3322 2 | 3
基于名为PKGINV
的第二个表,如下所示:
----------------------------------------
PKGNUMBER | BOARDNUMBER
----------------------------------------
NA13ALPK1010 | NA13ALANA1010
NA13ALPK1065 | NA13ALANA106
首先,我希望能够使用某种类型的联接进行SELECT
,以便我可以UNITS
显示STOCK
PKGNUMBER
和BOARDNUMBER
在PKGINV
中{1}}。
然后,我想UNITS
使用STOCK
PKGINV.PKGNUMBER = STOCK.NUMBER
PKGINV.BOARDNUMBER UNITS
更新STOCK
{/ 1}}。
因此,在更新后,NA13ALPK1010
将UNITS
11
(来自UNITS
的{{1}})NA13ALANA1010
将NA13ALPK1065
UNITS
1 {}} 5
(来自UNITS
的{{1}})。
先谢谢你的帮助!
答案 0 :(得分:1)
您的SELECT只需加入STOCK表两次:
SELECT
PKGNUMBER_Stock.UNITS AS PKGNUMBER_UNITS
,BOARDNUMBER_Stock.UNITS AS BOARDNUMBER_UNITS
FROM
PKGINV AS pkginv
JOIN STOCK AS PKGNUMBER_Stock ON PKGNUMBER_Stock.NUMBER = pkginv.PKGNUMBER
JOIN STOCK AS BOARDNUMBER_Stock ON BOARDNUMBER_Stock.NUMBER = pkginv.BOARDNUMBER
同样,UPDATE语句应该只匹配上面的连接集:
UPDATE PKGNUMBER_Stock
SET PKGNUMBER_Stock.UNITS = BOARDNUMBER_Stock.UNITS
FROM
PKGINV AS pkginv
JOIN STOCK AS PKGNUMBER_Stock ON PKGNUMBER_Stock.NUMBER = pkginv.PKGNUMBER
JOIN STOCK AS BOARDNUMBER_Stock ON BOARDNUMBER_Stock.NUMBER = pkginv.BOARDNUMBER
答案 1 :(得分:0)
您需要将Stock
加入PKGINV
两次:
Update s
set s.UNITS = s2.UNITS
from STOCK s
join PKGINV p on s.NUMBER = p.PKGNUMBER
join STOCK s2 on p.BOARDNUMBER = s2.NUMBER