Question

Is it possible to do a circular buffer using SQL?

I mean, using table myTable:

+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
+----+

Something like

SELECT id FROM `myTable` ORDER BY `id` LIMIT 5 OFFSET 3

would actually return

+----+
| id |
+----+
|  4 |
|  5 |
|  1 |
|  2 |
|  3 |
+----+
Was it helpful?

Solution

If I'm understanding your question correctly, you're trying to maintain the restriction of the limit and start ordering by the offset? If so, something like this could perhaps work:

  select id
  from mytable
  order by !(id>3), id
  limit 5

I've altered the input to include a 6th record to show the limit functionality.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top