Use LIMIT to paginate results in MySQL query - mysql

I want to fetch my results a 'page' at a time; I want the page number to be a parameter (in a JDBC prepared statement). Consider the following snippet
SELECT * FROM thread t ORDER BY t.id LIMIT ((? - 1) * 20), 20
So ideally, this would result, for page 1, to LIMIT 0, 20.
When I test
SELECT * FROM thread t ORDER BY t.id LIMIT ((1 - 1) * 20), 20
I am told I have a syntax error. I don't see what it could be, though - it's just some simple math. All it tells me is
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '((1 -
1) * 20), 20' at line 1
What am I doing wrong with my LIMIT clause, and how can I fix it?

This cannot be done.
See solution here:
MySQL Math and COUNT(*) in LIMIT
I would recommend using javascript or something to handle the first parameter (i.e. offset) such as:
limit 0,20 on first page and limit 21,20 on second...
For example if your first page has a get variable in the url www.example.com?page=1
offset = (page - 1)*20 ;
row_count = 20;
select * from table limit (offset, row_count);

Define Offset for the query using the following syntax
SELECT column FROM table
LIMIT {someLimit} OFFSET {someOffset};
For example, to get page #1 (records 1-10), set the offset to 0 and the limit to 10;
SELECT column FROM table
LIMIT 10 OFFSET 0;
To get page #2 (records 11-20), set the offset to 10 where the limit to 10
SELECT column FROM table
LIMIT 10 OFFSET 10;

MySQL requires numeric constants for that LIMIT syntax.
From http://dev.mysql.com/doc/refman/5.7/en/select.html:
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:
Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.
Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables.
Compute the constant on the Java side.

Related

get records from table starting from row 5 onwards

I have a mysql Database and I want to get the records starting from row 5 onwards. This is what i am sing right now
SELECT * FROM categories WHERE status = 1
limit 5 18446744073709551615 ORDER BY sortOrder ASC
But it is throwing me an error
Error Executing Database Query.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '18446744073709551615
ORDER BY
sortOrder ' at line 6
Also i am not sure at this point if this is the correct way of doing it or not
Thanks
You require OFFSET
Example:
SELECT something FROM table LIMIT $limit OFFSET $offset;
//or alternatively
SELECT something FROM table LIMIT $offset,$limit;
Regarding your error:
Remove the number - what is it for in the first place?
Your statement should be:
SELECT * FROM categories WHERE status = 1
OFFSET 5 ORDER BY sortOrder ASC
You missed a comma , in your sql query.. just change it to the following:
SELECT * FROM categories WHERE status = 1
limit 5, 18446744073709551615 ORDER BY sortOrder ASC
↑
Explanation: LIMIT with two arguments (comma separated) will take first argument as OFFSET and second as the maximum number of rows to return.
Alternatively, if you just want to get all rows from a particular record.. you can use OFFSET keyword. The following will fetch rows starting from 5th row.
SELECT * FROM categories WHERE status = 1
OFFSET 5 ORDER BY sortOrder ASC
Perhaps You need to just Rearrange the sequence of Instruction.
Make Sure 'sortOrder' column exists in your Categories table.
The Following Query should work,
Select * from Categories Where status =1
ORDER BY sortOrder ASC
Limit 4, 999980090909
If you want to get the rows starting from 5 , then your Limit value should start from 4 and the second value can be some larger number.
Hope this helps you!

MySQL: LIMIT 2,-1

$sql="SELECT * FROM `table_name` LIMIT 1,-1";
Why I get an error report:
SELECT * FROM `table_name` LIMIT 2,-1
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1' at line 1
from https://dev.mysql.com/doc/refman/5.0/en/select.html
The LIMIT clause can be used to constrain the number of rows returned
by the SELECT statement. LIMIT takes one or two numeric arguments,
which must both be nonnegative integer constants (except when using
prepared statements).
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;
so in your case
$sql="SELECT * FROM table_name LIMIT 1,18446744073709551615";
The LIMIT clause is used in the SELECT statement to constrain the number of rows in a result set. The LIMIT clause accepts one or two arguments. The values of both arguments must be zero or positive integer constants.
since you use negative(-1) number and hence that error is shown.

How to dynamicly assing LIMIT in MySQL?

I have query
SELECT *
LIMIT 3, (6 - 3)
Wchich returns:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6 - 3)' at line 2
I basicly want to select 6 rows * from 3-rd row, in MSSQL it's WHERE RowNumber BETWEEN 3 and 6 but dynamicly knowing :from and :to parameters instaed of :from and :size
USE OFFSET in this case
SELECT name
FROM your_db
ORDER BY your_column DESC
LIMIT 10 OFFSET 10
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constant.
Basically if you're not in a stored procedure or a prepared statement you can't do it.
Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.
Within stored programs, LIMIT parameters can be specified using
integer-valued routine parameters or local variables
So, inside a stored procedure, the following would work:
declare from_row bigint;
declare to_row bigint;
SELECT name
FROM your_db
ORDER BY your_column DESC
LIMIT from_row OFFSET (to_row-from_row);

sql syntax error while using top query

I'm using the top query for my table but facing the error
You have an error in your sql syntax, chekck the manual that
corresponds to your mysql server version for the right sytntax to use
near '4 * from sitemain order by siteid desc limit 0,30' at line 1
here is the code which i used
SELECT top 4 *
FROM sitemain
ORDER BY siteid DESC
You are mixing MySQL and TSQL syntax together. The query obviously is MySQL (from the error message). What you want is
SELECT * FROM sitemain ORDER BY siteid DESC LIMIT 0,4
What you loking for is actually LIMIT Clause,
The LIMIT clause can be used to constrain the number of rows returned
by the SELECT statement. LIMIT takes one or two numeric arguments,
which must both be nonnegative integer constants (except when using
prepared statements).
With two arguments, the first argument specifies the offset of the
first row to return, and the second specifies the maximum number of
rows to return. The offset of the initial row is 0 (not 1):
Documentation:
https://dev.mysql.com/doc/refman/5.0/en/select.html
SELECT *
FROM sitemain
ORDER BY siteid DESC
LIMIT 4
SELECT *
FROM sitemain
ORDER BY siteid DESC
LIMIT 4
With MySQL you need to use the LIMIT command as explained here:
Limit is used to limit your MySQL query results to those that fall within a specified range. You can use it to show the first X number of results, or to show a range from X - Y results. It is phrased as Limit X, Y and included at the end of your query. X is the starting point (remember the first record is 0) and Y is the duration (how many records to display).
SELECT *
FROM sitemain
ORDER BY siteid DESC
LIMIT 4

MySQL Is doing subquery after LIMIT syntax possible? If not, why?

I have MySQL Server version 5.1.53. I was looking for an hour to answer this question by myself. Including read the documentation itself at http://dev.mysql.com/doc/refman/5.1/en/select.html
Currently, I run this query.
SELECT dv2.timestamp
FROM data_val AS dv2
WHERE dv2.timestamp > '2011-06-10 22:26:25' ORDER BY dv3.timestamp DESC
LIMIT 1
Then I was trying to eliminate the ORDER BY syntax by determining the calculation of MAX_QUERIES minus 1. By doing that I could write,
SELECT (COUNT(*)-1) total
FROM data_val AS dv2a
WHERE dv2a.timestamp > '2011-06-10 22:26:13'
Finally the query becomes,
SELECT dv2.timestamp
FROM data_val AS dv2
WHERE dv2.timestamp > '2011-06-10 22:26:13'
LIMIT (
SELECT (COUNT(*)-1) total
FROM data_val AS dv2a
WHERE dv2a.timestamp > '2011-06-10 22:26:13'
), 1
And the error is:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '( SELECT (COUNT(*)-1) total FROM data_val AS dv2a ' at line 4
I also tried to put the subquery after OFFSET syntax. but still error.
Do you have any idea why my sub-query doesn't work?
I need technical details with short,
simple, and clean explanation.
From the MySQL manual: http://dev.mysql.com/doc/refman/5.5/en/select.html
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:
Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.
Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.
The MySQL query optimizer needs to resolve the limit parameters to a constant before running the query, or it will not know how many rows to return.
You can't imbed a query result for a limit parameter