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
2010-07-06 05:25:30
Source: http://dev.mysql.com/doc/refman/5.1/en/select.html
denrk
2010-07-06 05:29:32
The premise being to use a well-defined value for the upper bound; in this case an unsigned 64-bit integer.
Chris Hutchinson
2010-07-06 05:48:06
thank you, I've got to resolve this in my SQL query using INNER JOIN itself..
Zen
2010-07-19 06:51:34
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
2010-07-19 08:18:48
@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
2010-10-21 09:51:03
A:
Hi you can either pass the N
value by Query string or some cookie.
"SELECT * FROM tblProduction LIMIT 1,".$_GET[Limit];
VAC-Prabhu
2010-07-06 05:26:09
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
2010-07-06 05:41:25
Or just use prepared statements, which takes care of strings as well without needing careful sanitization.
Lèse majesté
2010-07-06 06:29:03
thank you, I've got to resolve this in my SQL query using INNER JOIN itself..
Zen
2010-07-19 06:50:56