查询不想工作得很好^^

时间:2013-03-29 03:51:39

标签: sql sql-delete

DELETE FROM _Inventory 
where ItemID = ( 
    SELECT * 
    FROM _Items 
    WHERE RefItemID IN ( 46440 , 46441 , 46442 ) 
        AND ID64 = @ID64 )

当EXISTS没有引入子查询时,只能在选择列表中指定一个表达式。

4 个答案:

答案 0 :(得分:1)

您需要使用IN并指定项目表中的字段(而不是指定*):

DELETE FROM _Inventory 
WHERE ItemID IN (
    SELECT ItemId 
    FROM _Items 
    WHERE RefItemID IN ( 46440 , 46441 , 46442 ) AND ID64 = @ID64 )

答案 1 :(得分:1)

只需使用IN

即可
DELETE 
FROM _Inventory 
where ItemID IN ( SELECT ItemID 
                  FROM _Items 
                  WHERE RefItemID IN (46440 ,46441 ,46442) 
                        AND ID64 = @ID64 
                )

或使用我更喜欢的JOIN

DELETE  a
FROM    _Inventory a
        INNER JOIN _Items b
            ON a.ItemID = b.ItemID
WHERE   b.RefItemID IN (46440, 46441, 46442) AND
        b.ID64 = @ID64

答案 2 :(得分:0)

应使用正确的列名替换星号。还要确保子查询只返回一行。不是多行。如果有多行,则需要将等同性比较改为IN子句。

答案 3 :(得分:0)

将其更改为SELECT ItemID FROM _Items ...。错误非常明确:不允许子查询返回多个列,因为ItemID只能与单个列进行比较。在EXISTS中,这相当于返回10,这仍然是一个列,因此也允许。