我有桌子销售和库存
促销
idStock
1000
1000
1001
1002
1002
1003
股票
idStock sold
1000 0
1001 0
1002 0
1003 0
如何像这样更新库存表(计算总销售商品并更新库存中的已售商品列)
idStock sold
1000 2
1001 1
1002 2
1003 1
答案 0 :(得分:0)
UPDATE stock
SET stock.sold = sale_1.myCount
FROM (SELECT idStock, count(*) as myCount
FROM sale
GROUP BY idStock) as sale_1
INNER JOIN stock on sale_1.idStock = stock.idStock
我是从记忆中做到的。我希望它有效。请告诉我们。