MySQL Query返回Web项目的匹配传出和返回航班

时间:2012-03-13 20:41:27

标签: php mysql

我刚刚开始学习MySQL并且无法从我的数据库中提取匹配的航班(我花了几天尝试不同的解决方案无济于事!)。我的数据库包含虚构的航班数据,其中出境和返程航班是相隔1周的倍数。

我希望设计一个查询,检查是否存在,然后在用户选择的出发日期的+/- 3天内提取出境和回程航班对。我需要通过出境航班订购航班然后匹配返回航班,因为我将在循环时使用PHP的mysql_fetch_array输出结果。

到目前为止,我一直在尝试使用IF(EXISTS(WHERE子句中的SELECT条件来检查出境航班是否存在,如果是真的(确实存在),则返回出站和返程航班。这适用于测试一个日期,但我不确定如何将它集成到日期范围,除非我可以在mysql中使用while循环?我还读过在WHERE子句中使用IF语句是不理想的,因为它增加了查询时间

返回日期以PHP计算,方法是在用户表单中添加'duration'选项(即1周,2周等)到该日期。

我已复制了目前为止的查询代码,但我很欣赏可能有更简单/更好的方法来实现我想要的结果,并感谢任何指导。我的代码将仅在提供给它的特定日期返回一对匹配的航班,因为我不确定如何实施+/- 3天搜索并保持结果按出站和返程航班对的顺序。

谢谢, 加里

SELECT 
sched.flight_schedule_id, dep_airport, dest_airport, code, dep_time, arr_time, flight_time, dep_date, dep.airport_name, dep.airport_country, adult_flight_price, dest.airport_name, dest.airport_country, plane.no_seats, sum(adult_seats_reserved), sum(child_seats_reserved)

        FROM 
            flight fli

            INNER JOIN flight_schedule sched
            ON fli.flight_id = sched.flight_id

            INNER JOIN airport dep
            ON fli.dep_airport = dep.airport_code

            INNER JOIN airport dest
            ON fli.dest_airport = dest.airport_code

            INNER JOIN plane_type plane
            ON fli.plane_id = plane.plane_id

            LEFT JOIN flight_inventory inv
            ON sched.flight_schedule_id = inv.flight_schedule_id    

        WHERE 

        IF (EXISTS(SELECT
            sched.flight_schedule_id, dep_airport, dest_airport, code, dep_time, arr_time, flight_time, dep_date, dep.airport_name, dep.airport_country, adult_flight_price, dest.airport_name, dest.airport_country

            FROM 
                flight fli

                INNER JOIN flight_schedule sched
                ON fli.flight_id = sched.flight_id

                INNER JOIN airport dep
                ON fli.dep_airport = dep.airport_code

                INNER JOIN airport dest
                ON fli.dest_airport = dest.airport_code

                LEFT JOIN flight_inventory inv
                ON sched.flight_schedule_id = inv.flight_schedule_id

            WHERE 


            dep.airport_name = '$to' AND dest.airport_name = '$from' AND sched.dep_date = '$return'),

            (dep.airport_name = '$from' AND dest.airport_name = '$to' AND sched.dep_date = '$outdate')

        OR

            (dep.airport_name = '$to' AND dest.airport_name = '$from' AND sched.dep_date = '$return'), '')

        GROUP BY

            sched.flight_schedule_id

1 个答案:

答案 0 :(得分:0)

这可能会对你有所帮助。这是一个新的解决方案,可以解决您的问题。我没有在您的查询中包含大量JOIN s,但添加它们是微不足道的。为了使+/- 3天工作,我假设您的dep_date属于TIMESTAMP类型,而您的$outdate$return也属于unix timestampsABS只是绝对值,即ABS(-x) = ABS(x) = x

SELECT
    * 
FROM 
    flight_schedule outbound
JOIN
    flight_schedule return 
    ON
        /* return flights should depart from outbounds destination */
        outbound.dest_airport = return.dep_airport
    AND
        /* optional. might reduce intermediate join result size */
        outbound.arr_date < return.dep_date 

WHERE
    /* user wants to fly from airport $from to airport $to */
    outbound.dep_airport = '$from'
    AND outbound.dest_airport = '$to'

    /* outbound flight should depart within 3 days before and after $outdate (3 days = 259200 seconds) */
    AND ABS(outbound.dep_date - $outdate) < 259200

    /* return flight should depart within 3 days before and after $return */
    AND ABS(return.dep_date - $return) < 259200

ORDER BY
    /* order outbound flights by distance to user's requested $outdate */
    ABS(outbound.dep_date - $outdate) ASC,

    /* and return flights by distance to user's requested $return date */
    ABS(return.dep_date - $return) ASC

如果列DATE的类型为dep_date,您仍然可以使用BETWEEN

WHERE outbound.dep_date BETWEEN
        DATE_SUB($outdate, INTERVAL 3 DAY)
    AND DATE_ADD($outdate, INTERVAL 3 DAY)

使用MySQL的DATE_ADDDATE_SUB函数或DATEDIFF

WHERE
    ABS(DATEDIFF(outbound.dep_date, $outdate)) < 259200

并使用DATEDIFF进行排序

ORDER BY 
    ABS(DATEDIFF(outbound.dep_date, $outdate)) ASC,
    ABS(DATEDIFF(return.dep_date, $return)) ASC