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 7652221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:42:45+00:00 2026-05-31T11:42:45+00:00

I have been trying to write a query for a public transportation system which

  • 0

I have been trying to write a query for a public transportation system which will output a list of routes when I input the origin and destination stop numbers.

Here is my MySQL Table :

mysql> desc route_timings;
+----------------+---------+------+-----+---------+----------------+
| Field          | Type    | Null | Key | Default | Extra          |
+----------------+---------+------+-----+---------+----------------+
| ID             | int(11) | NO   | PRI | NULL    | auto_increment |
| route_number   | int(11) | NO   |     | NULL    |                |
| stop_number    | int(11) | NO   |     | NULL    |                |
| arrival_time   | time    | YES  |     | NULL    |                |
| departure_time | time    | YES  |     | NULL    |                |
+----------------+---------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

Here are sample values :

mysql> select * from route_timings;
+----+--------------+-------------+--------------+----------------+
| ID | route_number | stop_number | arrival_time | departure_time |
+----+--------------+-------------+--------------+----------------+
|  1 |           54 |           1 | 10:00:00     | 10:05:00       |
|  2 |           54 |           2 | 11:00:00     | 11:05:00       |
|  3 |           54 |           3 | 12:00:00     | 12:05:00       |
|  4 |           55 |           3 | 13:00:00     | 13:05:00       |
|  5 |           55 |           4 | 14:00:00     | 14:05:00       |
|  6 |           55 |           5 | 15:00:00     | 15:05:00       |
|  7 |           60 |           3 | 10:00:00     | 10:05:00       |
|  8 |           60 |           2 | 11:00:00     | 11:05:00       |
|  9 |           60 |           1 | 12:00:00     | 12:05:00       |
+----+--------------+-------------+--------------+----------------+
9 rows in set (0.01 sec)

My question is: if I want to list the route_number’s that would contain stop_number 1 and stop_number 3, I would write a query similar to this :

SELECT DISTINCT `route_number` FROM `route_timings` WHERE `route_number` IN (
    SELECT `route_number` FROM `route_timings` WHERE `stop_number`=1
) AND `route_number` IN (
    SELECT `route_number` FROM `route_timings` WHERE `stop_number`=3
);

However, the above query would only return the route_numbers containing the two stops and not the routes where the source stop (1) would arrive before the destination stop(3).

The query would return the following:

+--------------+
| route_number |
+--------------+
|           54 |
|           60 |
+--------------+

The route_number 60 does not start from 1 and go to 3 but instead starts from 3 and goes to 1. Can someone please help me add that bit to the query so that the query outputs only the route_numbers where the arrival_time for the stop_number 1 is less than the arrival_time for the stop_number 3.

Thanks in advance.
-Shain

  • 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-31T11:42:46+00:00Added an answer on May 31, 2026 at 11:42 am

    How about something like:

    select fromStop.RouteNumber  
    from routeTimings fromStop  
      inner join routeTimings toStop on toStop.RouteNumber = fromStop.RouteNumber and toStop.StopNumber = 3 and toStop.ArrivalTime > fromStop.DepartureTime  
    where fromStop.StopNumber = 1
    

    Note this is using SQL server syntax and names adjusted to the convention I’m used to but the principle should be clear

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to write a regex that will remove whitespace following a
I'm trying to write a custom SQL query that will create a list of
I am trying to write a query that will search a list of strings
I have been trying to write a small app with its own option windows.
I have been trying to write a bare-bones ping scanner using Perl for internal
I have been trying to write an image on a layer using Quartz but
I have been trying to write my own diff3 wrap script for SVN and
I am new to programming. I have been trying to write a function in
Right now I have been trying to use Launchpad's API to write a small
I'm trying to write a query for a database that will left join a

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.