- 积分
- 5
- UID
- 3040
- 阅读权限
- 10
- 注册时间
- 2009-10-21
- 精华
- 在线时间
- 小时
- 最后登录
- 1970-1-1
- 职业
- 1
|
表 flight字段price,from,to,date,...(价格,出发地,目的地,日期)
我想得到出发地from=‘A’,到其他地方(目的地to不重复)的最低价格和相关信息
我写了个sql查询语句,可是太费时了,大家给给意见,如何提高速度
当价格price和目的地to都相同的,取时间最小的一个,这个由程序完成,主要怎么提取出到到其他地方的最低价格这条信息的其他字段值
感谢各位了,
Select f1.price,f1.to,f1.date
from flight f1
where f1.price=
(
Select min(f2.price)
from flight f2
where f2.from='A' and f2.to=f1.to
group by f2.to
)
and f1.from='A'
order by f1.price ASC,f1.date ASC
Select f1.price,f1.to,f1.date
from flight f1
where Exists
(
Select min(f2.price)
from flight f2
where f2.from='A' and f2.to=f1.to
group by f2.to
having min(f2.price)=f1.price
)
and f1.from='A'
order by f1.price ASC,f1.date ASC |
|