2

以下是我编写的一个 sql 查询,用于删除放置表中的所有记录,其中placement.op_number= 返回记录的列表。

子查询单独工作正常,只返回OP_NUMBER列(两行)。

我得到的错误是:

子查询最多可以返回一条记录

DELETE
FROM PLACEMENT
WHERE PLACEMENT.OP_NUMBER = (SELECT OP_NUMBER
                             FROM Opening
                             WHERE opening.qual_code = "SEC-45");

我究竟做错了什么?

4

3 回答 3

5

使用IN而不是等于:

delete
from PLACEMENT
where PLACEMENT.OP_NUMBER in (
        select OP_NUMBER
        from Opening
        where opening.qual_code = "SEC-45"
        );
于 2012-05-17T19:30:34.143 回答
1
DELETE
FROM PLACEMENT
WHERE EXISTS (SELECT OP_NUMBER FROM Opening
               WHERE opening.qual_code = "SEC-45" 
              AND OP_NUMBER = PLACEMENT.OP_NUMBER);
于 2012-05-17T19:30:57.320 回答
1

当前,您正在将 PLACEMENT.OP_NUMBER 与记录集进行比较 - Int 与记录集。您将需要使用 IN 关键字来将条件与多个值进行比较

您的新查询应如下所示 -

DELETE
FROM PLACEMENT
WHERE PLACEMENT.OP_NUMBER IN (SELECT OP_NUMBER
                             FROM Opening
                             WHERE opening.qual_code = "SEC-45");
于 2012-05-17T19:33:16.043 回答