Answer the question
In order to leave comments, you need to log in
How to make a JOIN so that 1 record from another table is attached to the main table, with certain conditions, from the part in the ORDER plan?
An example is a table of posts and comments.
You need to display a list of posts + by the most recent comment on the post.
Or a list of posts + by the most rated comment. Posts should not be repeated as a result.
I'd love to be able to represent this with CakePHP find, but a simple query would work too.
Answer the question
In order to leave comments, you need to log in
In general, everything is much simpler than it seemed ...
SELECT MAX(с.created)
FROM comments c
GROUP BY c.post_id
select * from posts p join comments c on c.post_id = p.id having max(c.date)
SELECT * FROM posts JOIN LEFT comments ON comments.post_id = posts.id GROUP BY posts.id ORDER BY comment.date DESC
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question