我试图在网上搜索答案,不幸的是它无法解决问题。
这是我的代码:
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号线上。它说缺少右括号。我尝试添加其他的,但问题仍然存在。
附言
我是一名目前正在学习Oracle SQL的学生,所以我的知识仍然缺乏。
您的陈述中有几个错误
- 要加入多个条件,您必须使用 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');
正确的查询应该是
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。
试试这个更清楚
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';