start2004 发表于 2009-10-21 13:54:06

[求助]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

start2004 发表于 2009-10-21 14:11:59

主要问如何提高速度

kider 发表于 2009-10-21 17:43:34

试试explain分析一下

edwin_chen 发表于 2009-10-22 10:19:28

能否给你的相关表结构(包括索引)

start2004 发表于 2009-11-12 10:58:02

问题已经解决,呵呵,忘记来结贴子了

xhw1982 发表于 2009-11-15 21:33:04

页: [1]
查看完整版本: [求助]MySql查询语句