Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select next rows in sql [closed]

I have more records in my database. so I want to select first 10,000 record. Then i have to continue the selection from next records means next 10,001 to till some values. How to put query here?

Any idea means helpful to me.

like image 402
Duk Avatar asked Oct 18 '25 18:10

Duk


2 Answers

If you havent ID row in your table, or ID isn't IDENTITY, you can use ROW_NUMBER function like this:

SELECT ROW_NUMBER() OVER (ORDER BY Products.ProductID) as 'rownbr', Products.ProductID
FROM Products
ORDER BY Products.ProductID

and then just use WHERE clause like this:

SELECT ROW_NUMBER() OVER (ORDER BY Products.ProductID) as 'rownbr', Products.ProductID
FROM Products
WHERE rownbr BETWEEN 10000 and 20000
ORDER BY Products.ProductID
like image 130
Timofey G Morozov Avatar answered Oct 20 '25 08:10

Timofey G Morozov


First 10,000 Records and then rest of records;

SELECT TOP 10000 * FROM table ORDER BY Id
DECLARE @count int;
SET @count = (SELECT COUNT(*)  FROM table);
SELECT TOP (@count - 10000) * FROM table ORDER BY Id DESC;
like image 45
Mehmet Ince Avatar answered Oct 20 '25 09:10

Mehmet Ince



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!