I have 3 tables :
Post, PostComment and Comment.
It’s a many-to-many relation.
I want to select for each post the last comment.
So something like (select * from comment order by create_at DESC limit 1) doest not work here.
And I want something like:
select *
from post as p
left join post_comment as pc on (pc.post_id = p.id)
left joint comment as c on (c.id = pc.comment_id)
left joint comment as c2 on (c2.id = pc.comment_id and c2.id > c.id)
where c2.id is null
It works very well for one-to-many relation, but I can’t get ride of this for many-to-many.
Note: I renamed my table. In my code, I do not use comment and post. And I do need a many-to-many relation.
Thanks you
I did something like that :