MySQL: select the most recent poster in a forums thread list -
i have tried many of similar questions available on stack, find of them continually user_name of wrong user, stuck , have spent whole day trying figure out.
i have forum, , within forum list of threads, , within threads people can post, when user views forum, , sees list of threads, want them recent post , user's name display
this current sql query:
select t.thread_id, t.featured, t.title, t.post_count, t.view_count, t.closed, p.post_id latest_reply, if(u2.user_name not null, u2.user_name, u.user_name) latest_reply_user_name, max(p.timestamp) timestamp, u.user_name forum_thread t left join forum_post p on(t.thread_id = p.thread_id , p.post_id=(select max(p.post_id) forum_post thread_id=p.thread_id)) left join forum_post p2 on (p2.post_id = p.post_id) left join user u on(t.id = u.id) left join user u2 on (p2.id = u2.id) t.forum_id = :forum_id , t.sticky=:sticky , t.removed=0 group t.thread_id order latest_reply desc limit :limit_bottom, :limit_top
the problem u2.user_name appears selected randomly , therefore latest_reply_user_name selected randomly. u2.user_name should the user_name of recent post.
your subquery should be:
(select max(p.post_id) forum_post thread_id=t.thread_id)
so highest post id in each thread.
Comments
Post a Comment