0

Example. I need to get list of posts and match 1 comment to each one (newest, oldest or most rated).

Are there any suggestions?

SELECT *
FROM posts p
LEFT JOIN comments c
ON p.id = c.post_id
GROUP BY p.id

And now I need to set, which one from comments (top, newest or oldest) must be matched with post. But I really don't know how to.

Taras Kudrych
  • 31
  • 1
  • 1
  • 8

1 Answers1

0

Newest:

SELECT MAX(с.created) ........
FROM comments c
GROUP BY c.post_id

Oldest:

SELECT MIN(с.created) ........
FROM comments c
GROUP BY c.post_id

Most rated:

SELECT MAX(с.rating) ........
FROM comments c
GROUP BY c.post_id
Taras Kudrych
  • 31
  • 1
  • 1
  • 8