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
Related
$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.
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);
I'm used to running on an Oracle database, so I'm not really quite sure how to trouble shoot this problem. I've narrowed down a simple example of my query to the following:
SELECT 0 as gm_rowID,
'-ALL Grantmakers-' as grantmakerName
FROM dual
GROUP BY 2
phpMyAdmin runs the SQL with the following error:
#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 'ORDER BY 2 LIMIT 0, 30' at line 1
Oracle can run this query just fine. MySQL can run the query without the GROUP BY clause. Any ideas?
--Here is the entire query:
SELECT
p.grantmaker_rowid as gm_rowID,
gm.grantmaker_companyName as grantmakerName
FROM grantmaker_info gm, proposal_submission p
WHERE 0=0
AND p.grantmaker_rowid = gm.grantmaker_rowid
UNION
SELECT
0 as gm_rowID,
'-ALL Grantmakers-' as grantmakerName
FROM dual
ORDER BY 2
GROUP BY 2
LIMIT 0 , 30
Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column
names, column aliases, or column positions. Column positions are
integers and begin with 1
From: http://dev.mysql.com/doc/refman/5.0/en/select.html
Unless you only have 1 column in that table, it should run fine. My suggestion however would be to reference the column name (or alias) of whatever you're trying to GROUP BY.
edit: My only other suggestion is to include the SHOW CREATE TABLE output for that table.
edit2: Ok I see you've updated your question. Why not instead of ORDER BY 2, you ORDER BY grantmakerName (if that's the column you want to order by?)
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.
So I asked a similar question yesterday, and got a great response - I'm hoping for the same luck this time around. What I'm trying to create is paged results for a MySQL query. I'm taking advantage of LIMIT. Here is my query.
SELECT
BookingInfo.ClinicID, BookingInfo.BookingDate, BookingInfo.BookingTime,
BookingInfo.Status, PatientBooking.FirstName, PatientBooking.LastName,
PatientBooking.DateOfBirth
FROM BookingInfo
LEFT JOIN PatientBooking
ON BookingInfo.PatientID = PatientBooking.PatientID
WHERE PatientBooking.FirstName = 'Brian'
AND PatientBooking.LastName = 'Lang'
AND (BookingInfo.ClinicID = '1' OR BookingInfo.ClinicID = '2')
LIMIT '0, 20'
ORDER BY BookingInfo.BookingDate DESC
This query does not work (However if I remove the LIMIT '0,20' part it works just fine). Instead I get this error returned: #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 ''0, 20' ORDER BY BookingInfo.BookingDate DESC' at line 1
Any ideas from the experts?
Your LIMIT clause should not be enclosed in quotes. It should just read LIMIT 0, 20. Quotes are usually reserved for character strings.
Also, the LIMIT clause must appear after the ORDER BY clause. See SheepSimulator's answer for details.
Ref
I think the limit should come after the order. But what about is not working?
Your ORDER clause should be placed before the LIMIT clause.
Check out the MySQL syntax table for SELECT.
The LIMIT clause should be after the ORDER BY clause.
It's format is LIMIT 0, 20 not LIMIT '0, 20'.
Reference: MySQL SELECT syntax.