I have a database with two tables: trips and days. Each entry in days has an id that corresponds to an entry in trips.
I want to run a query that will return the trip that occurs on a provided date, and if there isn’t one that occurs on that date then return the next trip after that date. Here’s my current query (daystring is generated in code, assume it’s a valid SQL date string):
SELECT _id, number, date
FROM trips
INNER JOIN days ON trips._id = days.trips_id
WHERE days.date > DATE( daystring, "start of day", "-1 day" )
AND days.date < DATE( daystring, "start of day", "+1 day" );
That query only returns a result if a trip exists on the provided date though. How can I modify it to return the next trip after that date only if there isn’t one on that date?
is a quick fix. Might want an index on the date column if you don’t have one though.