0

So say I'm making a social media app, with a table posts for holding posts, and a table post_likes for holding the likes for a certain post. Let's say I have a column date_posted in table posts that is a bigint, and holds unix timestamp values.

In order to paginate, I need to know the offset, limit, and the starting point for each fetch from the database. The limit and starting point are held constant, and of course, only the offset changes when you page more and more.

So say I paginate once (with posts ordered by date_posted DESC), so I grab the most recent posts first. Let's say (hypothetically) the most recent post I get initially has a date_posted of 10000. Therefore, on subsequent pages, I limit the posts to having a date_posted <= 10000, because otherwise, it will grab newer posts, which is not what I want - I want only older posts.

How would I do this with post_likes? I query

SELECT COUNT(*) FROM posts_likes WHERE posts.id = post_likes.post_id

to get the number of likes for each post. Is there any way to paginate by these values, such as ordering by most popular posts?

The main problem I am having is the "starting point". With date_posted, it was easy to set a benchmark with just a WHERE clause,

...WHERE date_posted <= 10000...

to avoid getting newer posts, where date_posted would be greater than 10000. How do I avoid getting newer posts with the number of likes?

Example:

posts
id | date_posted
0      100
1      120
2      300
3      1400

post_likes
id | user_id | post_id
0     uid1       2
1     uid1       3
2     uid2       2
3     uid10      2
4     uid22      3
5     uid4       1

So say I want to get all posts ordering by most recent, with a limit of 2

SELECT * FROM posts ORDER BY date_posted DESC LIMIT 2 OFFSET 0

Which would give me

posts
id | date_posted
3      1400
2      300

Paging this, saving the start position as 1400 because anything larger than that is a newer post, with

SELECT * FROM posts WHERE date_posted <= 1400 ORDER BY date_posted DESC LIMIT 2 OFFSET 2

would give me the next two.

My question is, how do I choose the start position when sorting by most popular posts? My query looks like this:

SELECT * FROM posts ORDER BY (SELECT COUNT(*) FROM posts_likes WHERE posts.id = post_likes.post_id) DESC LIMIT 2 OFFSET 0

But when I page, how do I stop from getting newer posts, since paging means you only want to get older posts?

NOTE: You can replace the whole post_likes table with a num_likes column in the posts table, if that makes it easier. It's the same problem.

  • Please show some sample data to make what you want clearer. – Tim Biegeleisen Dec 13 '18 at 4:55
  • If I didn't misunderstand, you could just include the primary key in the ORDER BY clause to give you a unique condition: WHERE (date_posted, id) < (10000, 751956) – Laurenz Albe Dec 13 '18 at 5:32
  • @TimBiegeleisen I added a sample, does this work? – Michael Hsu Dec 13 '18 at 5:32
  • Doesn't help much. You seem to be using two types of query here. One selects posts, and the other aggregates to find count of likes. Figure out what you need help with and then ask. – Tim Biegeleisen Dec 13 '18 at 5:33
  • @TimBiegeleisen I don't need help with the one that sorts and pages posts by date, I need the help with the one that sorts and pages by the number of likes. – Michael Hsu Dec 13 '18 at 5:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.