Venkatesh
Venkatesh

Reputation: 108

Get records after a certain record (id) in MySQL

I want to get records after a certain record, say id is 4.
Actually I want to show posts based on likes with pagination.

So I sorted result by likes.
But I don't know how to get the next records based on id.

Here is my code

SELECT urp.* , likes
FROM user_related_posts urp
JOIN (
    SELECT post_id, COUNT(*) AS likes
    FROM post_likes
    WHERE STATUS =1
    GROUP BY post_id
    ) v ON urp.post_id = v.post_id
GROUP BY post_id
ORDER BY likes DESC 
LIMIT 0 , 30

Sorry I don't put image, I don't have enough credentials.

What here doing is when I order with likes, ids are unsorted, but I want records based on id. Image Link

Image Link

Upvotes: 1

Views: 693

Answers (2)

anjnkmr
anjnkmr

Reputation: 868

You can use LIMIT so that you can retrieve it like pagination

Upvotes: 1

Tharif
Tharif

Reputation: 13971

You can use date_created in your query.

SELECT        *
FROM            post_likes
WHERE        (date_created >
                             (SELECT        date_created
                               FROM            post_likes AS p1
                               WHERE        (id= 4)))

Note :

I have not added the entire code.Please use the code and edit as your requirement.

Upvotes: 0

Related Questions