如果标题没有准确描述问题(它可能没有),请道歉。我有以下两个表: -
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吗?
答案 0 :(得分:1)
您的查询将不 SELECT ID FROM tPieceTable WHERE (BatchNo = 'ABC') AND (PieceNo = 'X1')
返回多个ID
试试这个;
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'))