这适用于使用Oracle DB 11g的数据库类
Tables:
• Flight (flt_no, from_city, to_city, flt_distance,
flt_departs, flt_arrives, flt_price)
• Aircraft (craft_id, craft_name, cruising_range)
• Employee (emp_id, emp_name, emp_salary)
• Certified (emp_id, craft_id)
QUERY:客户希望从麦迪逊前往纽约,航班变更不超过两次。如果客户想在下午6点之前抵达纽约,请列出从麦迪逊出发的选择。
任何帮助都很感激。我真的不知道从哪里开始。
修改 的 这是我到目前为止所提出的。如果我在正确的轨道上,请告诉我,至少请。
SELECT F.flt_no
FROM Flight F
WHERE F.from_city = 'Madison'
AND F.to_city = 'New York'
AND DATEPART(hh, F.flt_arrives) <= 18
UNION
SELECT F.flt_no
FROM Flight F
WHERE (F.from_city = 'Madison'
AND F.to_city IN (SELECT from_city
FROM Flight F
WHERE F.to_city = 'New York')
)
OR
(F.to_city = 'New York'
AND F.from_city IN (SELECT to_city
FROM Flight F
WHERE F.from_city = 'Madison')
AND DATEPART(hh, F.flt_arrives) <= 18
)
答案 0 :(得分:6)
想想这三种情况:
现在开始逐步完成案件。
对于1.寻找开始和停止的航班,你知道何时何地你知道对不对?
For 2.想想那两个航班。他们将在哪里开始和结束,这两个航班的共同点是什么。每次飞行的时间标准是什么?
For 3.考虑这3个细分和每个细分的特征,包括依赖
如果要显示所有选项,那么你最终想要所有这些的联合。
此功能:选择to_char(DATE,'HH24') may help.
答案 1 :(得分:0)
正如@Michael Durrant给出的答案我只想以SQL格式提供解决方案。希望这会有所帮助。请检查不等于签名。
#Direct flight
select f.no
from flights f
where f.arrives=18:00 and (f.from='madison' and f.to='NewYork')
union
# Cases for one connecting flight
select f2.no
from flights f1, flights f2
where f2.arrives=6pm and
f1.from='Madison' and f1.to<>'New York'
and f1.to=f2.from and f2.to='New York'
and f2.depart> f1.arrives
and f2.arrives<'18:00'
union
#Cases involving two connecting flights
select f3.no
from flights f1,flights f2, flights f3
where f1.from='Madison'
and f1.to=f2.from
f1.to<>'New York'
f2.to=f3.from
f2.to<>'New York'
and f3.to='New York'
and f2.depart> f1.arrives
and f3.depart> f2.arrives
and f3.arrives<'18:00'