[求助]MySql查询语句
表 flight字段price,from,to,date,...(价格,出发地,目的地,日期)我想得到出发地from=‘A’,到其他地方(目的地to不重复)的最低价格和相关信息
我写了个sql查询语句,可是太费时了,大家给给意见,如何提高速度
当价格price和目的地to都相同的,取时间最小的一个,这个由程序完成,主要怎么提取出到到其他地方的最低价格这条信息的其他字段值
感谢各位了,:handshake
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 主要问如何提高速度 试试explain分析一下 能否给你的相关表结构(包括索引) 问题已经解决,呵呵,忘记来结贴子了 select * from flight where from = 'A' group by to having min(price)
页:
[1]