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 |
+----+
有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top