sql语句有错误

时间:2014-07-08 15:52:11

标签: sql oracle

我收到错误但是问题是什么。请帮助我解决这个问题。

 select Train_name from TRAIN where Train_id in 
(select Train_id from ROUTE where source_distance in (select source_distance from ROUTE         where stop_number in 
  (select Station_id from STATION where Station_name='Kulaura Railway Station'))  <   source_distance in
      (select source_distance from ROUTE where stop_number in 
          (select Station_id from STATION where Station_name='Saistagonj Railway Station')));

错误:

ORA-00907: missing right parenthesis 
*Cause: 00907. 00000 - "missing right parenthesis" 
*Action: Error at Line: 73 Column: 87

1 个答案:

答案 0 :(得分:0)

删除额外)<

试试这个

SELECT train_name 
FROM   train 
WHERE  train_id IN (SELECT train_id 
                    FROM   route 
                    WHERE  source_distance IN (SELECT source_distance 
                                               FROM   route 
                                               WHERE 
                           stop_number IN (SELECT station_id 
                                           FROM   station 
                                           WHERE 
                           station_name = 'Kulaura Railway Station') AND 
                                   source_distance IN (SELECT source_distance 
                                                       FROM   route 
                                                       WHERE 
                                   stop_number IN (SELECT station_id 
                                                   FROM   station 
                                                   WHERE 
                                   station_name = 'Saistagonj Railway Station')) 
                   ); 

而不是SubQuery您可以使用JOIN

在大多数情况下,JOIN比子查询更快,并且子查询的速度非常快。

JOIN中,RDBMS可以创建一个更适合您的查询的执行计划,并且可以预测应该加载哪些数据进行处理并节省时间,这与子查询不同,它将运行所有查询和加载所有数据以进行处理。

子查询的好处在于它们比JOIN更具可读性:这就是大多数新SQL用户更喜欢它们的原因;这是简单的方法;但是在性能方面,JOINS在大多数情况下都更好,即使它们也不难阅读。

SELECT T.train_name  
FROM train T JOIN route R ON R.train_id = T.train_id 
             JOIN station St On St.station_id = R.stop_number
WHERE St.station_name = 'Kulaura Railway Station' OR St.station_name = 'Saistagonj Railway Station'