views:

42

answers:

2

I am confuse on how to limit selection in MySQL (e.g SELECT * FROM tblProduction LIMIT 1,N;) where N is unknown.. Can anyone help me on how can I limit the selection in MySQL? I want to show the records starting from row 2 (two) up to the end of the records. Thanks!

+5  A: 

This is from the LIMIT documentation:

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:

SELECT * FROM tbl LIMIT 95,18446744073709551615;

EDIT: So in your case you would just change 95 to 2. Then you would get all rows starting from the 2 result.

spinon
Source: http://dev.mysql.com/doc/refman/5.1/en/select.html
denrk
The premise being to use a well-defined value for the upper bound; in this case an unsigned 64-bit integer.
Chris Hutchinson
thank you, I've got to resolve this in my SQL query using INNER JOIN itself..
Zen
I'm not sure I understand what you mean? Can you edit the question with the sql query you are trying to run or some pseudo code of what you would like it to do?
spinon
@zen, if this was the most helpful answer, you should accept it (click the tick/check beneath the vote counter). People will be more inclined to answer your questions if you have a high accept rate
fearoffours
A: 

Hi you can either pass the N value by Query string or some cookie.

"SELECT * FROM tblProduction LIMIT 1,".$_GET[Limit];
VAC-Prabhu
Nasty practice to drop the GET straight in like that
alex
1) Doesn't answer the the question. 2) It's little Bobby Tables time...
Pete
Expanding on @alex comment, best practice would be to try-cast the HTTP-GET query string ($_GET) to an integer. In this case, parsing for injection attacks is pointless because that value should only ever be an integer.
Chris Hutchinson
Or just use prepared statements, which takes care of strings as well without needing careful sanitization.
Lèse majesté
thank you, I've got to resolve this in my SQL query using INNER JOIN itself..
Zen