ORACLE SQL错误 - 缺少右括号 - 查询

时间:2012-12-14 15:03:51

标签: sql oracle ora-00907

我试图在网上和周围寻找答案,不幸的是它无法解决问题。

这是我的代码:

SELECT d.driverID, 
       e.firstname, 
       e.lastname, 
       t.testid, 
       t.testType
 FROM driver d
 LEFT JOIN employee e 
   ON (e.employeeID = d.employeeID)
INNER JOIN driver_test dt 
   ON (d.driverID = dt.driverID  WHERE dt.testDate(BETWEEN TO_DATE('01-JAN-2012', 'dd-mm-yyyy') AND TO_DATE('31-JAN-2012', 'dd-mm-yyyy')))
RIGHT JOIN test t ON dt.testID = t.testID WHERE (t.testType='Alcohol');

问题在于第4行。它说缺少右括号。我尝试添加其他的但问题仍然存在。

P.S

我是一名正在学习Oracle SQL的学生,所以我的知识仍然缺乏。

3 个答案:

答案 0 :(得分:1)

您的正确查询应

SELECT d.driverID, e.firstname, e.lastname, t.testid, t.testType
FROM driver d
LEFT JOIN employee e ON(e.employeeID = d.employeeID)
INNER JOIN driver_test dt ON(d.driverID = dt.driverID and dt.testDate(BETWEEN TO_DATE('01-JAN-2012', 'dd-mm-yyyy') AND TO_DATE('31-JAN-2012', 'dd-mm-yyyy')))
RIGHT JOIN test t ON dt.testID = t.testID 
WHERE (t.testType='Alcohol');

Where子句用于过滤,在这种情况下,您将连接逻辑操作(在连接中)。所以你只需要使用AND。

答案 1 :(得分:1)

您的陈述中有几处错误

  • 要加入多个条件,您必须使用AND,而不是WHERE
  • 摆脱(在列名和BETWEEN之间

修正查询:

SELECT d.driverID,
   e.firstname,
   e.lastname,
   t.testid,
   t.testType
FROM driver d
  LEFT JOIN employee e
    ON (e.employeeID = d.employeeID)
 INNER JOIN driver_test dt
    ON (d.driverID = dt.driverID AND
       dt.testDate BETWEEN TO_DATE('01-JAN-2012',
                                    'dd-mm-yyyy') AND
       TO_DATE('31-JAN-2012',
                'dd-mm-yyyy'))
 RIGHT JOIN test t
    ON dt.testID = t.testID
 WHERE (t.testType = 'Alcohol');

答案 2 :(得分:0)

尝试这个更清楚

SELECT d.driverID, e.firstname, e.lastname, t.testid, t.testType
FROM driver d
LEFT JOIN employee e ON(e.employeeID = d.employeeID)
INNER JOIN driver_test dt ON(d.driverID = dt.driverID)
RIGHT JOIN test t ON dt.testID = t.testID
where dt.testDate(BETWEEN TO_DATE('01-JAN-2012', 'dd-mm-yyyy') AND 
TO_DATE('31-JAN-2012','dd-mm-yyyy'))) and t.testType='Alcohol';