MariaDB社区

标题: [求助]MySql查询语句 [打印本页]

作者: start2004    时间: 2009-10-21 13:54
标题: [求助]MySql查询语句
表 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
作者: start2004    时间: 2009-10-21 14:11
主要问如何提高速度
作者: kider    时间: 2009-10-21 17:43
试试explain分析一下
作者: edwin_chen    时间: 2009-10-22 10:19
能否给你的相关表结构(包括索引)
作者: start2004    时间: 2009-11-12 10:58
问题已经解决,呵呵,忘记来结贴子了
作者: xhw1982    时间: 2009-11-15 21:33
select * from flight where from = 'A' group by to having min(price)




欢迎光临 MariaDB社区 (http://123.56.88.72/) Powered by Discuz! X3.2