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
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...
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With