SQL语句问题(不是IN)

时间:2018-11-06 06:43:56

标签: sql oracle

通过单项作业(直到最后期限),无法弄清楚我为什么/为什么误使用NOT IN功能。

SELECT DISTINCT P1.TITLE, P1.PANUM, D1.POSTCODE
FROM PAPER P1
INNER JOIN AUTHOR A1 ON A1.PANUM = P1.PANUM
INNER JOIN ACADEMIC A2 ON A2.ACNUM = A1.ACNUM
INNER JOIN DEPARTMENT D1 ON D1.DEPTNUM = A2.DEPTNUM
WHERE P1.PANUM NOT IN(
    SELECT DISTINCT P1.TITLE, P1.PANUM, D1.POSTCODE
    FROM PAPER P1
    INNER JOIN AUTHOR A1 ON A1.PANUM = P1.PANUM
    INNER JOIN ACADEMIC A2 ON A2.ACNUM = A1.ACNUM
    INNER JOIN DEPARTMENT D1 ON D1.DEPTNUM = A2.DEPTNUM
    WHERE D1.POSTCODE LIKE '4%'
)
ORDER BY P1.PANUM;

目的是从4开始的邮政编码中创建一个没有作者的P1.TITLE列表。

我收到ORA-00913:值太多错误,无法完全理解原因。

3 个答案:

答案 0 :(得分:1)

只关注您的问题:

  

目的是创建一个P1.TITLE列表,该列表中没有作者   从4开始的邮政编码。

您可以简单地执行以下操作:

SELECT TITLE, PANUM
FROM PAPER
WHERE NOT EXISTS (
    SELECT 1
    FROM AUTHOR
    INNER JOIN ACADEMIC ON AUTHOR.ACNUM = ACADEMIC.ACNUM
    INNER JOIN DEPARTMENT ON ACADEMIC.DEPTNUM = DEPARTMENT.DEPTNUM
    WHERE AUTHOR.PANUM = PAPER.PANUM
    AND DEPARTMENT.POSTCODE LIKE '4%'
)
ORDER BY PANUM

答案 1 :(得分:0)

您必须在内部子查询中选择一列

SELECT DISTINCT P1.TITLE, P1.PANUM, D1.POSTCODE
FROM PAPER P1
INNER JOIN AUTHOR A1 ON A1.PANUM = P1.PANUM
INNER JOIN ACADEMIC A2 ON A2.ACNUM = A1.ACNUM
INNER JOIN DEPARTMENT D1 ON D1.DEPTNUM = A2.DEPTNUM
WHERE P1.PANUM NOT IN(
    SELECT  P1.PANUM
    FROM PAPER P1
    INNER JOIN AUTHOR A1 ON A1.PANUM = P1.PANUM
    INNER JOIN ACADEMIC A2 ON A2.ACNUM = A1.ACNUM
    INNER JOIN DEPARTMENT D1 ON D1.DEPTNUM = A2.DEPTNUM
    WHERE D1.POSTCODE LIKE '4%'
)
ORDER BY P1.PANUM;

答案 2 :(得分:0)

使用IN和NOT IN关键字时,这意味着其中仅一个参数(或列)。