Sql内连接不存在子查询错误

时间:2017-03-25 14:01:18

标签: sql oracle

SQL>   SELECT CUSTOMER.CUSTOMERID,
  2    CUSTOMER.CUSTOMERNAME,
  3    EVENT.EVENTLOCATION
  4    WHERE NOT EXISTS
  5    (SELECT * FROM EVENT.EVENTLOCATION AND EVENTLOCATION ="LONDON 02")
  6    FROM CUSTOMER
  7    INNER JOIN BOOKING
  8    ON BOOKING.CUSTOMERID = CUSTOMER.CUSTOMERID
  9    INNER JOIN EVENT
 10    ON EVENT.EVENTID=BOOKING.EVENTID
 11    INNER JOIN CONCERTS
 12    ON CONCERTS.CONCERTID=EVENT.CONCERTID;
  WHERE NOT EXISTS
  *
ERROR at line 4:
ORA-00923: FROM keyword not found where expected

在尝试使用内部联接查找尚未参加伦敦活动的客户时,我在第4行收到错误。我哪里错了。

2 个答案:

答案 0 :(得分:0)

错误是因为您将exists语句放在选择列表中,它应该位于WHERE条件中。我把它改成了如下

SELECT 
    CUSTOMER.CUSTOMERID,
    CUSTOMER.CUSTOMERNAME,
    EVENT.EVENTLOCATION
FROM CUSTOMER
    INNER JOIN BOOKING
        ON BOOKING.CUSTOMERID = CUSTOMER.CUSTOMERID
    INNER JOIN EVENT
        ON EVENT.EVENTID=BOOKING.EVENTID
    INNER JOIN CONCERTS
        ON CONCERTS.CONCERTID=EVENT.CONCERTID;
WHERE NOT EXISTS
(
    SELECT 1 FROM EVENT.EVENTLOCATION WHERE EVENTLOCATION ='LONDON 02'
)

答案 1 :(得分:0)

首先,最好在所有连接后放置“WHERE”子句。 第二,你的“SELECT”条款

SELECT * FROM EVENT.EVENTLOCATION AND EVENTLOCATION ="LONDON 02"

错了。语法是:

SELECT * FROM TABLE_NAME WHERE YOUR_CONDITIONS

所以也许是这样的:

SELECT CUSTOMER.CUSTOMERID,
CUSTOMER.CUSTOMERNAME,
FROM CUSTOMER
LEFT OUTER JOIN BOOKING
ON BOOKING.CUSTOMERID = CUSTOMER.CUSTOMERID
LEFT OUTER JOIN 
(SELECT * FROM EVENT WHERE EVENTLOCATION="LONDON 02") AS LONDON_EVEN
ON EVENT.EVENTID=BOOKING.EVENTID
INNER JOIN CONCERTS
ON CONCERTS.CONCERTID=EVENT.CONCERTID
WHERE EVENT.EVENTID IS NULL;