SQL - 删除和子查询

时间:2012-05-17 19:29:06

标签: sql relational-database embedded-sql

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

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

我得到的错误是:

  

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

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

我做错了什么?

3 个答案:

答案 0 :(得分:5)

使用IN代替等于:

delete
from PLACEMENT
where PLACEMENT.OP_NUMBER in (
        select OP_NUMBER
        from Opening
        where opening.qual_code = "SEC-45"
        );

答案 1 :(得分:1)

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

答案 2 :(得分: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");