SQL语句问题

时间:2010-01-05 17:04:59

标签: sql if-statement

我有一个火车公司的数据库。我想将两个查询组合在一起,这样我就可以将它们用作Java前端的一个。

第一个查询返回未满的列车的ID。

select TrainID 
  from Train 
 where Capacity > 0;

每列火车的容量为50,每次预订座位时我减1,所以如果容量大于零,那么船上就有座位。

第二个查询返回给定目的地和原点的火车的RouteID:

select * 
  from Timetable 
 where RouteID = (select RouteID 
                    from Routes 
                   where OriginID = "New York" 
                     and DestinationID = "LA");

问题/需要

我想将这两个查询合并在一起,所以我可以这样:

Give me all the Timetable entries for this route ONLY IF there's room on the train.

我不是一个DB的人,所以我只是把两个和两个放在一起。

编辑:架构概述如下。谢谢你到目前为止的答案,我现在就试试吧。

路线

+---------------+-------------+------+-----+---------+-------+
| Field         | Type        | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| RouteID       | varchar(25) | NO   | PRI | NULL    |       |
| OriginID      | varchar(25) | NO   | MUL | NULL    |       |
| DestinationID | varchar(25) | NO   | MUL | NULL    |       |
| Duration      | int(3)      | NO   |     | NULL    |       |
+---------------+-------------+------+-----+---------+-------+

+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| StationID | varchar(25) | NO   |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

时刻表

+-------------+-------------+------+-----+---------------------+----------------+
| Field       | Type        | Null | Key | Default             | Extra          |
+-------------+-------------+------+-----+---------------------+----------------+
| TimeID      | int(11)     | NO   | PRI | NULL                | auto_increment |
| RouteID     | varchar(11) | NO   |     | NULL                |                |
| TrainID     | varchar(11) | NO   |     | NULL                |                |
| DepartDate  | timestamp   | NO   |     | 0000-00-00 00:00:00 |                |
| ArrivalDate | timestamp   | NO   |     | 0000-00-00 00:00:00 |                |
+-------------+-------------+------+-----+---------------------+----------------+

列车

]+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| TrainID  | varchar(11) | NO   | PRI | NULL    |       |
| Capacity | int(11)     | NO   |     | 50      |       |
+----------+-------------+------+-----+---------+-------+

5 个答案:

答案 0 :(得分:3)

Select * from Timetable ti 
inner join Train tr 
    ON ti.TrainID = tr.ID 
    (Or whatever link there is between the two tables)
WHERE ti.RouteID in 
    (select RouteID from Routes where OriginID="New York" 
    and DestinationID="LA") 
AND tr.Capacity > 0

答案 1 :(得分:1)

您尚未说明TrainID与路线或时间表的关系。需要某种关系来执行此任务。

如果是标准关系那么......

select t.* 
from Timetable t, Routes r, Trains tr
where r.OriginID ='New York' 
and r.DestinationID ="LA"
and t.RouteID = r.RouteID
and tr.RouteID = t.RouteID
amd tr.Capacity > 0;

此致 克里斯

答案 2 :(得分:0)

假设时间表中有一个trainid,那么这应该是关闭的

select a.*
from Timetable a
inner join Train b on a.trainid = b.trainid and b.capacity > 0
where a.RouteID =
     (
     select RouteID
     from Routes
     where OriginID = "New York" and DestinationID="LA"
     );

答案 3 :(得分:0)

稍微猜测您的架构:

SELECT  t.*
FROM    Timetable t
        INNER JOIN
                Routes r
                ON t.RouteID = r.RouteID
        INNER JOIN
                Trains tr
                ON t.TrainID = tr.TrainID
WHERE   r.OriginID = 'New York'
AND     r.DestinationID = 'LA'
AND     tr.Capacity > 0

加入Routes而不是使用子查询可能会表现得更好。

答案 4 :(得分:0)

我将假设TrainID存储在Routes表中,这在问题中并不清楚。

select * from Timetable where RouteID in 
(select RouteID from Routes 
 inner join Train on Train.TrainID = Routes.TrainID
where 
  OriginID = "New York" 
  and DestinationID = "LA"
  and Capacity > 0;
);