I need to write a query that will figure out the previous/next day from today that has a record in the table, and then get all rows for that day.
When data is inserted, the days can differ. For example, just because it is tuesday does not mean the previous day is monday. The previous day could be sunday, saturday, or even wednesday of last week.
I’m trying to figure out how to best select the previous and next day (that is not today) that has a record. Then also, in the same query, get all of the rows for that day.
I don’t know if there is a function or anything for this, I’m pretty stumped. I know how to do this with 2 queries, but I want to do it with 1. Any help would be greatly appreciated.
The following queries return the events in the nearest day, either in the past or in the future:
Given the sample schema:
ORDER BYin conjuction withLIMIT 1does the trick. Use>for the nearest in the future, and<for nearest in the past (and reverse the ordering). You may want to add indices on theschedulecolumn to improve the performance.