Mysql - Select next x records even if reaching rowset end I have a table with an ID field, unique, autoincremental.

I need to, given certain ID, retrieve next 3 records even when given ID is the last one.  

Table:

ID       
1         
2         
3         
4         
5         
6         
7     


What I need:  

Given ID is 1 =&gt; retrieve 2,3,4  
Given ID is 2 =&gt; retrieve 3,4,5 


.....  

Given ID is 5 =&gt; retrieve 6,7,1  
Given ID is 6 =&gt; retrieve 7,1,2  
Given ID is 7 =&gt; retrieve 1,2,3  


How can I do this?
Let me try for a pure SQL solution:

SELECT id
FROM  
   (  ( SELECT *, 0 AS chck
        FROM tableX
        WHERE id &gt; @givenID
        ORDER BY id
        LIMIT 3
      )
    UNION ALL
      ( SELECT *, 1 
        FROM tableX
        WHERE id &lt;= @givenID      -- just in case of a small table (&lt; 3 rows) 
        ORDER BY id
        LIMIT 3
      )
   ) AS t
ORDER BY chck, id
LIMIT 3 ;


Test at SQLfiddle.



The query is basically an improvement on Jehad's answer. It doesn't use a variable (@rank) and the two internal LIMITs will result in improved efficiency on a large table. 

Assuming there is an index on id, the two subqueries will use the index and only need to fetch 3 ids and 3 rows each from the table. The external table (t) after the UNION will have a maximum of 6 rows to sort.