D
D
Denis2015-12-18 08:21:22
PHP
Denis, 2015-12-18 08:21:22

How to select records from the database cyclically?

Help me figure it out.
Given
a database with entries being added and removed.
The entries are grouped into categories.
Roughly speaking, one category of records is a stack of paper sheets.
There are tens of thousands of entries in each category.
Required: take the topmost sheet from the stack, return it to the calling script, and put the sheet itself at the very bottom of the stack. and so on in a circle.
I can not figure out how to do it logically, and as a result, technically.
There was an idea to prescribe serial numbers for each position, and after selecting an element, update all numbers in the database. But it's kind of hard.
There was an idea to mark the last selected element with a flag (similar to the cursor). But it is not clear what to do if the marked element is removed from the database.
I feel that there is a simple solution, but I can not figure it out.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2015-12-18
@cjbars

Add `lastUse` TIMESTAMP field with index;

UPDATE `table` SET @id := `id`, `lastUse` = NOW() ORDER BY `lastUse` LIMIT 1;
SELECT * FROM `table` WHERE `id` = @id;

This combination of requests also allows several scripts to work in parallel.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question