Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql query for table row range

Tags:

mysql

This might be a very basic question but I am struggling with queying the specific rows in a table based only on the row range.

Let's say I have a table ABC where I have populated 1000 rows. Now I want a sql query so that I can fetch first 100 rows ( that is the range 1 to 100) and then the next 100 ( 101 to 200) and so on till I am done with all rows. And this should be done without querying/filtering on the table's id or any column id.

I am not able to figure it out as I am trained only on querying specific columns in WHERE clause so would appreciate if someone can plz help

like image 603
Shyam Avatar asked May 22 '13 19:05

Shyam


People also ask

How do I find the range of a row in MySQL?

You have to use the LIMIT clause in the SELECT query. MySQL allows you to set two parameters for the clause, the offset (first parameter) and the number of rows to fetch (second parameter). SELECT * FROM `ABC` LIMIT 0, 100 SELECT * FROM `ABC` LIMIT 100, 100 SELECT * FROM `ABC` LIMIT 200, 100 -- etc...

How do I find the range of a row in SQL?

You can do this by using ROW_NUMBER () function provided by Sql server. your table name. It will retrieve the records from rows 10 to 20 from your table.

How do I select a range in MySQL?

IN(start,end): It means that the intermediate value between start and end won't get displayed. For the above logic, you can use BETWEEN. BETWEEN clause is inclusive, for example, suppose there are 1,2,3,4,5,6 numbers.


1 Answers

You have to use the LIMIT clause in the SELECT query. MySQL allows you to set two parameters for the clause, the offset (first parameter) and the number of rows to fetch (second parameter).

SELECT * FROM `ABC` LIMIT 0, 100
SELECT * FROM `ABC` LIMIT 100, 100
SELECT * FROM `ABC` LIMIT 200, 100
-- etc...

However, you cannot guarantee the order of these rows unless you sort by one or more specific column(s) using the ORDER BY clause.

Read more about the SELECT statement here: http://dev.mysql.com/doc/refman/5.6/en/select.html

like image 55
rink.attendant.6 Avatar answered Oct 26 '22 23:10

rink.attendant.6