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

  • Home
  • SEARCH
  • 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 789423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:31:23+00:00 2026-05-14T21:31:23+00:00

I need to retrieve data from a specific time period. The query works fine

  • 0

I need to retrieve data from a specific time period.
The query works fine until I specify the time period. Is there something wrong with the way I specify time period? I know there are many entries within that time-frame.

This query returns empty:

SELECT  stop_times.stop_id, STR_TO_DATE(stop_times.arrival_time, '%H:%i:%s') as stopTime, routes.route_short_name, routes.route_long_name, trips.trip_headsign  FROM trips 
JOIN stop_times ON trips.trip_id = stop_times.trip_id 
JOIN routes ON routes.route_id = trips.route_id
WHERE  stop_times.stop_id = 5508
HAVING stopTime BETWEEN DATE_SUB(stopTime,INTERVAL 1 MINUTE) AND  DATE_ADD(stopTime,INTERVAL 20 MINUTE);

Here is it’s EXPLAIN:

+----+-------------+------------+--------+------------------+---------+---------+-------------------------------+------+-------------+
| id | select_type | table      | type   | possible_keys    | key     | key_len | ref                           | rows | Extra       |
+----+-------------+------------+--------+------------------+---------+---------+-------------------------------+------+-------------+
|  1 | SIMPLE      | stop_times | ref    | trip_id,stop_id  | stop_id | 5       | const                         |  605 | Using where |
|  1 | SIMPLE      | trips      | eq_ref | PRIMARY,route_id | PRIMARY | 4       | wmata_gtfs.stop_times.trip_id |    1 |             |
|  1 | SIMPLE      | routes     | eq_ref | PRIMARY          | PRIMARY | 4       | wmata_gtfs.trips.route_id     |    1 |             |
+----+-------------+------------+--------+------------------+---------+---------+-------------------------------+------+-------------+
3 rows in set (0.00 sec)

The query works if I remove the HAVING clause (don’t specify time range). Returns:

+---------+----------+------------------+-----------------+---------------+
| stop_id | stopTime | route_short_name | route_long_name | trip_headsign |
+---------+----------+------------------+-----------------+---------------+
|    5508 | 06:31:00 | "80"             | ""              | "FORT TOTTEN" |
|    5508 | 06:57:00 | "80"             | ""              | "FORT TOTTEN" |
|    5508 | 07:23:00 | "80"             | ""              | "FORT TOTTEN" |
|    5508 | 07:49:00 | "80"             | ""              | "FORT TOTTEN" |
|    5508 | 08:15:00 | "80"             | ""              | "FORT TOTTEN" |
|    5508 | 08:41:00 | "80"             | ""              | "FORT TOTTEN" |
|    5508 | 09:08:00 | "80"             | ""              | "FORT TOTTEN" |

I am using Google Transit format Data loaded into MySQL.

The query is supposed to provide stop times and bus routes for a given bus stop.
For a bus stop, I am trying to get:

  1. Route Name
  2. Bus Name
  3. Bus Direction (headsign)
  4. Stop time
    The results should be limited only to buses times from 1 min ago to 20 min from now.

Please let me know if you could help.

UPDATE
The problem was that I was comparing DATE to DATETIME as one answer said.
I could not use DATE because my values had times but not dates.
So my solution was to use Unix time:

 SELECT  stop_times.stop_id, stop_times.trip_id,   UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(NOW(),'%Y-%m-%d '), stop_times.arrival_time)) as stopTime, routes.route_short_name, routes.route_long_name, trips.trip_headsign  FROM trips 
JOIN stop_times ON trips.trip_id = stop_times.trip_id 
JOIN routes ON routes.route_id = trips.route_id
WHERE  stop_times.stop_id = 5508
HAVING stopTime > (UNIX_TIMESTAMP(NOW()) - 60) AND stopTime < (UNIX_TIMESTAMP(NOW()) + (60*20));
  • 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-14T21:31:24+00:00Added an answer on May 14, 2026 at 9:31 pm

    Stoptime is a time value, and DATE_ADD/SUB work with datetime fields. Ensure they are both the same type.

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

Sidebar

Related Questions

No related questions found

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.