I have just started learning MySQL and am having trouble extracting matching flights from my database (I have spent days trying different solutions to no avail!). My database holds fictitious flight data where outbound and return flights are multiples of 1 week apart.
I am looking to design a query which checks for the existence of and then fetches pairs of outgoing and return flights for +/- 3 days of the departure date selected by the user. I need the flights to be ordered by outbound flight and then the matching return flight as I will be outputting the results with PHP’s mysql_fetch_array while loop.
So far I have been experimenting with IF (EXISTS(SELECT conditions within the WHERE clause to check whether the outbound flight exists, and if it is true (does exist) then return the outbound and return flight. This works ok for when testing for one date but I’m not sure how to integrate it into a date range unless I can use a while loop in mysql? I have also read it is not ideal to have IF statements within the WHERE clause as it increases the query time.
The return date is calculated in PHP by adding the ‘duration’ option in the user form (i.e. 1 week, 2 weeks etc) to the out date.
I have copied the query code I have so far below but I appreciate there may be a lot simpler/better way of achieving my desired result and would be grateful for any guidance. My code will return a matching pair of flights only for the specific date supplied to it as I’m not sure how to implement a +/- 3 day search AND keep the results in the order of outbound and return flight pairs.
Thanks,
Gary
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
This might help you. It’s a fresh solution that should solve your problem. I did not include the numerous
JOINs from your query, but adding them is trivial. To make the +/- 3 days part work, I assumed yourdep_dateto be of typeTIMESTAMPand your$outdateand$returnto be unix timestamps as well.ABSis just the absolute value, i.e.ABS(-x) = ABS(x) = xIf you have the type
DATEfor the columndep_dateyou can still use BETWEEN likeusing MySQL’s DATE_ADD and DATE_SUB functions or DATEDIFF
and sort using DATEDIFF as well