Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7647437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:27:37+00:00 2026-05-31T10:27:37+00:00

I have just started learning MySQL and am having trouble extracting matching flights from

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:27:38+00:00Added an answer on May 31, 2026 at 10:27 am

    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 your dep_date to be of type TIMESTAMP and your $outdate and $return to be unix timestamps as well. ABS is just the absolute value, i.e. 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
    

    If you have the type DATE for the column dep_date you can still use BETWEEN like

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

    using MySQL’s DATE_ADD and DATE_SUB functions or DATEDIFF

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

    and sort using DATEDIFF as well

    ORDER BY 
        ABS(DATEDIFF(outbound.dep_date, $outdate)) ASC,
        ABS(DATEDIFF(return.dep_date, $return)) ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started learning MVVM and having a dilemna. If I have a
I have just started learning django. I created a form from django models. On
G'Day Programmers, I am from Java background however I have just started learning C++
I have just started learning Erlang and am trying out some Project Euler problems
I have just started learning python and am getting caught up. I come from
I have just started learning ruby reading from different resources. One of them is
I have just started learning Rails and I'm trying to build a post/like type
I'm learning Java and just started playing around with mysql and am having a
I have just started learning MVVM. I've made the application from scratch by following
I have just started learning Jquery and am new to writing javascript (I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.