我有两个表: 表A = PIECE_ASSY
id Component_Ref BOM PieceNum
1 ITEM:MAGNET BOM1 CRCR1126
2 ITEM:FILLER BOM2 CRCR5425
3 ITEM:SCREW BOM1 CRCR2135
4 ITEM:MAGNET BOM2 CRCR1127
表B =件
id Name
CRCR1126 Magnet Assembly
CRCR5425 Filler Assembly
CRCR2135 Screw Assembly
CRCR1127 Magnet Assembly
现在,我正在尝试获取没有特定Component_Ref的部件ID的列表,直到现在我已经编写了此查询
SELECT id
from Piece
WHERE id NOT IN (SELECT p.id
from Piece p
inner join PIECE_ASSY pa ON pa.PieceNum = p.id
WHERE p.id IN ('CRCR1126','CRCR5425')
AND pa.Component_Ref = 'ITEM:MAGNET')
现在我期望的输出是,由于我通过了上述查询,所以上面的查询应该返回“ CRCR5425”(“ CRCR1126”,“ CRCR5425”),只有CRCR5425没有查询的Component_Ref,而是返回了我CRCR5425,CRCR2135
现在,我要获取的是没有Component_Ref的部件ID,而不是给我表B中的所有部件ID,它只应该向我输出在过滤器参数中传递的那些部件。
答案 0 :(得分:0)
这样的事情应该起作用(我希望)。
SELECT p.id
FROM Piece p
WHERE p.id IN ('CRCR1126','CRCR5425')
AND NOT EXISTS ( SELECT 1 FROM PIECE_ASSY pa WHERE pa.PieceNum=p.id AND pa.Component_Ref='ITEM:MAGNET' )
答案 1 :(得分:0)
您可以在下面尝试
SELECT id
from Piece p
WHERE id IN (SELECT PieceNum FROM PIECE_ASSY pa WHERE pa.PieceNum = p.id
AND p.id IN ('CRCR1126','CRCR5425') AND pa.Component_Ref <> 'ITEM:MAGNET')