Vicky Gill
Vicky Gill

Reputation: 734

How to Select every 5000th Number from DB in mysql

I have a database table named tickets, I want to fetch every 5000th Number , Like first will be id number 5000 2nd will be 10000 So on.

Upvotes: 2

Views: 158

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521194

If the IDs are not continuous you could always simulate row number:

SET @rn=0;
SELECT *
FROM
(
    SELECT *, @rn:=@rn+1 AS rn
    FROM yourTable
    ORDER BY id
) t
WHERE rn % 5000 = 0;

Here is a simplified demo with non continuous id values showing that this approach can work:

Demo

Upvotes: 5

grobmotoriker
grobmotoriker

Reputation: 479

If you have a numeric primary key (for example "id"), you could do something like this:

SELECT * FROM tickets WHERE id % 5000 = 0;

Upvotes: 6

Related Questions