The query selects the oldest row from a records table that’s not older than a given date. The given date is the last row queried which I grab from a records_queue table. The goal of the query is to rotate through the rows from old to new, returning 1 row at a time for each user.
SELECT `records`.`record_id`, MIN(records.date_created) as date_created
FROM (`records`)
JOIN `records_queue` ON `records_queue`.`user_id` = `records`.`user_id`
AND record_created > records_queue.record_date
GROUP BY `records_queue`.`user_id`
So on each query I’m selecting the oldest row min(date_created) from records and returning the next oldest row larger > than the given date from records_query. The query keeps returning rows until it reaches the newest record. At that point the same row is returned. If the newest row was reached I want to return the oldest (start again from the bottom – one full rotate). How is that possible using 1 query?
From the code you have posted, one of two things is happening. Either this query is returning a full recordset that your application is then able to traverse through using it’s own logic (this could be some variant of javascript if the page isn’t reloading or passing parameters to the PHP code that are then used to select which record to display if the page does reload each time), or the application is updating the
records_queue.record_dateto bring back the next record – though I can’t see any limitations of only fetching a single record in the query you posted.Either way, you will need to modify the application logic, not this query to achieve the outcome you are asking for.
Edit: In the section of code that updates the queue, do a quick check to see if the value in
records_queue.record_dateis equal to the newest record. If it is run something likeupdate records_queue set record_date = (select min(theDateColumn from records)instead of the current logic which just updates it with the current date being looked at.