문제

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