0

如果标题没有准确描述问题(它可能没有),请道歉。我有以下两个表:-

tLiveTable:

PieceID      LocationRef
------------------------
100              5

tPieceTable:

ID    BatchNo      PieceNumber
------------------------------
50    ABC          X1
100   ABC          X1

仅在给定 BatchNo 和 PieceNo 的情况下,如何更新 tLiveTable 中该片段的 LocationRef?

我尝试了以下方法:

UPDATE tLiveTable
SET LocationRef = 'blabla unimportant'
WHERE
PieceID = (SELECT ID FROM tPieceTable WHERE (BatchNo = 'ABC') AND (PieceNo = 'X1'))

但是 tPieceTable 中可以有多个条目具有相同的 BatchNo 和 PieceNo。我只想从 tPieceTable 中检索 ID,其 ID 当前是 tLiveTable 中的 ID。

我真的应该为此使用 INNER JOIN 吗?

4

1 回答 1

1

您的查询将无法返回多个 ID SELECT ID FROM tPieceTable WHERE (BatchNo = 'ABC') AND (PieceNo = 'X1')

尝试这个;

UPDATE L SET LocationRef = 'blabla unimportant'
FROM tLiveTable L JOIN tPieceTable P 
     ON L.PieceID = P.ID
WHERE (P.BatchNo = 'ABC') AND (P.PieceNo = 'X1')

IN用于=

UPDATE tLiveTable
SET LocationRef = 'blabla unimportant'
WHERE
PieceID IN (SELECT ID FROM tPieceTable WHERE (BatchNo = 'ABC') AND (PieceNo = 'X1'))
于 2012-12-12T16:58:27.867 回答