1

以下の select ステートメントを使用して、別のテーブルの製品の数量を更新しますtablex。このクエリの製品番号をproductnumber tablexに一致させ、このステートメントで見つかった数量をtablexの既存の数量に追加する方法を理解できないようです。

select 
    p.ProductNumber, sod.Quantity ,so.StateCode
from 
    SalesOrderDetail sod
right join 
    ProductAssociation pa on sod.ProductId = pa.ProductId
left join 
    Product p on pa.AssociatedProduct = p.ProductId
left join 
    SalesOrder so on so.SalesOrderId = sod.SalesOrderId
where 
    so.StateCode = '3'
4

5 に答える 5

0

複数のテーブルに基づいて更新することができます。構文は次のようなものです

update tablex
set tablex.quantity = sod.Quantity
from tablex join product p on tablex.productnumber = p.ProductNumber
join... -- add the rest of your joins and conditions.
于 2013-07-17T20:15:54.553 に答える
0

これを探していますか?

UPDATE tx SET tx.Quantity = tx.Quantity + sod.Quantity FROM 
from SalesOrderDetail sod
right join ProductAssociation pa on sod.ProductId=pa.ProductId
left join Product p on pa.AssociatedProduct=p.ProductId
left join SalesOrder so on so.SalesOrderId=sod.SalesOrderId
left join tablex tx on p.ProductNumer = tx.ProductNumber
where so.StateCode='3'

SQL で JOIN を使用して UPDATE ステートメントを実行するにはどうすればよいですか?

于 2013-07-17T20:16:04.207 に答える
0

UPDATEの基本構文JOIN:

UPDATE A
SET A.foo = B.bar
FROM TableA A
JOIN TableB B 
    ON A.col1 = B.colx

だから私はあなたが次のようなものを求めていると信じています:

UPDATE A
SET A.Quantity = B.Quantity + A.Quantity
FROM Tablex A
JOIN (select p.ProductNumber, sod.Quantity ,so.StateCode
      from SalesOrderDetail sod
      right join ProductAssociation pa on sod.ProductId=pa.ProductId
      left join Product p on pa.AssociatedProduct=p.ProductId
      left join SalesOrder so on so.SalesOrderId=sod.SalesOrderId
      where so.StateCode='3'
      )B
   ON A.ProductNumber = B.ProductNumber

あなたの StateCode がどのように考慮されているかわからない場合は、JOINおそらく追加の基準がありますか?

于 2013-07-17T20:16:21.430 に答える
0

大量の行を更新しようとしていると思われるTableXので、一度に 1 つずつではなく、すべてを一度に行う方法を提案します。

update TableX
 set quantity = quantity +
 (select sod.quantity from SalesOrderDetail where sod.ProductId = TableX.ProductId)

のサブセットでこれを実行したい場合がありますが、それで問題ありません。そのために句をSalesOrderDetail使用するだけです。WHERE

于 2013-07-17T20:17:32.263 に答える