get records from table starting from row 5 onwards - mysql

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!

Related

begin selection with second row of data

I need to begin my selection starting with the second row of data. This query works fine
SELECT Sum(ttimediff) as 'TOTAL IDLE TIME (sec)' FROM temp where ttimediff>#3;
I just need it start with the second row, and read all rows after that.
I have tried FROM temp ORDER BY asc LIMIT 2 where ttimediff>#3; but I believe the limit statement is stopping the selection at 2. I get a syntax error
You can do this with Limit but you need to have 2 Arguments like this :
SELECT * FROM table_name LIMIT 1, 5000
This will skip 1 element and check until 5000

MySQL - Limit with sub query

I want to limit selected all except 1st row.
Id is int, but not UNIQUE,
checkIn is date
SELECT * FROM table
Order by property_id, checkIn DESC
LIMIT 2, (SELECT Count(property_id)-1 FROM table)
Both Queries are working. but then I put them together i get
#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(property_id) FROM table)' at line 1
Maybe here is universal key for all rows at LIMIT 1, ALL ?
If you need all rows except the first one, try this:
SELECT * FROM TBL LIMIT 1, 18446744073709551615;
18446744073709551615 is the recommend as a value as in the mysql docs and the maximum of a unsigned bigint.
There was a similar question.
For completeness, here's how you can do it with a prepared statement:
SET #skip=1;
SET #numrows=(SELECT Count(property_id)-1 FROM table);
PREPARE STMT FROM 'SELECT * FROM table Order by property_id, checkIn DESC LIMIT ?, ?';
EXECUTE STMT USING #skip, #numrows;
Although, if it's a InnoDB table, I wouldn't recommend to do a SELECT COUNT(*) on it repeatedly. Unlike MyISAM it doesn't store a row count in the table. Therefore counting on InnoDB can be slow when it has lots of rows.
P.S.: Note, that you have to use limit 1, how_many instead of limit 2, how_many when you want to skip one row, since it starts counting from 0, not from 1.
Johan already mentioned here
You can not use a subquery as a LIMIT argument. Limit argument
should be an INTEGER. You subquery returns, wel... basically, a
table.
From the MySQL manual
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.
Edit
Try like this
but i'm not tested
DECLARE offset bigint;
SELECT Count(property_id)-1 INTO offset FROM table;
SELECT * FROM table Order by property_id, checkIn DESC LIMIT 2,offset;
I agree with #omeinusch's answer, but also offer the following alternative:
SELECT * FROM table t JOIN (
SELECT property_id, checkIn
FROM table
ORDER BY property_id, checkIn DESC
LIMIT 2,1
) s ON (t.property_id > s.property_id)
OR (t.property_id = s.property_id AND t.checkIn <= s.checkIn)
If you have a unique id column, then you could do this:
select * FROM table
where id <> (SELECT id FROM table Order by property_id, checkIn DESC LIMIT 1)
Otherwise, assuming property_id and checkIn are unique,
select * FROM table
where concat(property_id,'|',checkIn) <>
(SELECT concat(property_id,'|',checkIn) FROM table Order by property_id, checkIn DESC LIMIT 1)

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

SQL limit number of groups

I am trying to create a simple pagination but it seems to fail to limit the number of results.
SELECT * FROM visits GROUP by clientID ORDER BY 'date' LIMIT $from, $to
I want to get only the first visit (chronologically) of every client and paginate the results.
If I make this query with $from = 6, $to = 12 it returns like 8 rows instead of 7.
What I'm doing wrong?
From MySQL's docs on 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).
When you chose $from as 6 and $to as 12; you are not selecting from 6 to 12; you will be selecting 12 rows, starting from $from + 1 = 7.
The first argument to the LIMIT cluase is the starting offset and the second is the number of rows to return.
Therefore your query should be:
... LIMIT $from, ($to - $from)
With the dbms and syntax you're using, the second number in the limit expression is the number of results allowed, not offset endpoints. So you're starting at 6 and allowing the next 12 results, not getting results 6-12.
If you want to get results 6-12, use limit 5,7
Your query does not return the first visit by date. To do this, you need to actually join in this information:
select v.*
from visits v join
(select clientid, MIN(date) as mindate
from visits
group by clientid
) vd
on v.clientid = vd.clientid and v.date = vd.date
order by clientid
limit $from - 1, $to - $from
Your originally query is returning an arbitrary set of columns about a given client. These columns are not even guaranteed to be from the same record. This is because you are using a MySQL (mis)feature, where you can have columns in the select clause that are not in the group by clause and are not the arguments to an aggregation function.

Use LIMIT to paginate results in MySQL query

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.