The second table shows the wrong identifiers for the first two rows, by the way.
It should be:
id likes 1 2 3 2
This works in MySQL for me:
Select id, likes from (SELECT id, @rownum: =@rownum +1 AS rownum, likes FROM table u, (SELECT @rownum:=0) r ORDER BY likes) as derived where rownum >= 2 and rownum <= 4; (SELECT id, @rownum: =@rownum +1 AS rownum, likes FROM table u, (SELECT @rownum:=0) r ORDER BY likes);
The last part attempts to simulate a row number that is not available in MySQL, but is available in MSSQL, Oracle, and others.
source share