mySQL - LIMIT syntax to get all the rows after a certain point? - mysql

I hope this is a very easy answer:
I can use
LIMIT 2,4
to give me records 3,4,5,6
How can I get all the records from 3 onwards? If I use
LIMIT 2
It will just give me the first 2 rows? Right?

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 3rd row to the last:
SELECT * FROM tbl LIMIT 2,18446744073709551615;
Taken from the MySQL manual
SQLFiddle demo

Related

Why is this MySQL query correct?

I have a table which has only 3 columns. When I type the following query
select * from MyTable order by 5 and 2;
I get every thing in the table( The result is equal to that of select * from MyTable; ). What I originally expected is that I would get some kind of error. But I didn't get it, why?
order by 5 and 2 is interpreted as order by (5 and 2) which is a constant expression, hence no real ordering is done and data is simply shown in the order it was inserted.
What is happenning here is that 5 and 2 is seen as an expression which is evaluated to 1. However, it shouldn't give a result sorted by first column.
Actually, I think you only get sorted data because you inserted it in sorted sequence. Take a look at this SQLFiddle:
http://sqlfiddle.com/#!2/3e04e/1
The data is not sorted by any of the columns, it is being sorted by a value 1.
5 and 2 are column indexes and they mean 5th column and 2nd column.
select * from MyTable order by 5,2;
It means start with index 5 and bring 2record i.e 5,6,7

How does 'LIMIT' parameter work in sql?

I have 4000 rows for example, and I define X limit.
The query stops after it finds X rows? or the query finds all the rows and then takes X rows from the found rows?
Thank you.
From MySQL Reference Manual:
If you use LIMIT row_count with ORDER BY, MySQL ends the sorting as
soon as it has found the first row_count rows of the sorted result,
rather than sorting the entire result. If ordering is done by using an
index, this is very fast. If a filesort must be done, all rows that
match the query without the LIMIT clause must be selected, and most or
all of them must be sorted, before it can be ascertained that the
first row_count rows have been found. In either case, after the
initial rows have been found, there is no need to sort any remainder
of the result set, and MySQL does not do so.
So it looks like it's possible that the entire result set is known before the LIMIT is applied. But MySQL will try everything it can not to do so. And you can help it by providing useful indexes that match your queries.
EDIT: Furthermore, if the set is not sorted it terminates the SELECT operation as soon as it's streamed enough rows to the result set.
SELECT * FROM your_table LIMIT 0, 10
This will display the first 10 results from the database.
SELECT * FROM your_table LIMIT 5, 5
This will show records 6, 7, 8, 9, and 10
It's like telling MySql; I want you to start counting from 5+1 or the 6th record, but Select only upto 5 records
I'm assuming you're thinking about MySQL, in which according to the documentation, the answer is it depends. If you're using a LIMIT (without a HAVING), then:
If you are selecting only a few rows with LIMIT, MySQL uses indexes
in some cases when normally it would prefer to do a full table scan.
As soon as MySQL has sent the required number of rows to the client, it aborts the query unless you are using SQL_CALC_FOUND_ROWS.
There are a few other cases which you should read about in the documentation.
Introduction to MySQL LIMIT clause
The following illustrates the LIMIT clause syntax with two arguments:
SELECT
select_list
FROM
table_name
LIMIT [offset,] row_count;
The offset specifies the offset of the first row to return. The offset of the first row is 0, not 1.
The row_count specifies the maximum number of rows to return.
The following picture illustrates the LIMIT clause:
Therefore, these two clauses are equivalent:
> LIMIT row_count;
> LIMIT 0 , row_count;
The following picture illustrates the evaluation order of the LIMIT clause in the SELECT statement:
It stops after it found the number of rows specified in the LIMIT clause. This can be verified with a large amount of data. It retrieves the result in a time that is not possible if it is getting all the rows of the table and filtering after that.
If you are using MS SQL Server, then you can write it as given below.
Select TOP [x]
*
From MyTable
Hope it helps.
Vamyip

mysql limit - a general question

Assuming the query
SELECT * FROM table WHERE a='1' and b='2' and c>'3' and d>'4' and e!='5' and f='6'
returns 10000 results.
My question is, let's say I limit the search to the first 10 results like this:
SELECT * FROM table WHERE a='1' and b='2' and c>'3' and d>'4' and e!='5' and f='6' LIMIT 10
Will mysql search through all the 10000 results or it will stop at the 10th result?
LIMIT will only display the rows specified, based on their position within the resultset. Without an ORDER BY, you're relying on the order the records were inserted.
You'll probably be interested to read about MySQL's ORDER BY/LIMIT performance...
Since there is no ORDER BY, it will stop at the 10th result (after going through as many non-matching rows as necessary). As OMG Ponies says, which 10 rows you get is unspecified.

how can i limit the selection in MySQL?

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!
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.
Hi you can either pass the N value by Query string or some cookie.
"SELECT * FROM tblProduction LIMIT 1,".$_GET[Limit];

MySQL skip first 10 results

Is there a way in MySQL to have the first 10 result from a SELECT query skipped?
I'd like it to work something like LIMIT.
Use LIMIT with two parameters. For example, to return results 11-60 (where result 1 is the first row), use:
SELECT * FROM foo LIMIT 10, 50
For a solution to return all results, see Thomas' answer.
There is an OFFSET as well that should do the trick:
SELECT column FROM table
LIMIT 10 OFFSET 10
OFFSET is what you are looking for.
SELECT * FROM table LIMIT 10 OFFSET 10
From the manual:
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;
Obviously, you should replace 95 by 10. The large number they use is 2^64 - 1, by the way.
LIMIT allow you to skip any number of rows. It has two parameters, and first of them - how many rows to skip
To skip first 10 rows use OFFSET 10, but LIMIT is also required,
If you want to get all the rows and only skip first 10 rows try using a big number like 999999999 or as per your choice
SELECT * FROM table LIMIT 999999 OFFSET 10
select * from table where id not in (select id from table limit 10)
where id be the key in your table.
If your table has ordering by id, you could easily done by:
select * from table where id > 10