mysql - Using SQL joins to determine order of a post -
i trying fetch forum topics based on amount of replies has been submitted topic. new understanding how sql joins work, though have been using sql awhile in general, though feel on right track. have query:
select * `topics` inner join ( select count(tid) countedreplies,`tid` `replies` group `tid` ) on topics.id = replies.tid order replies.countedreplies desc
table structures this:
topics
id topic name 1 filler topic name 2 exciting posts excellence 3 random name example
replies
id tid post 1 3 hooray! wonderful! 2 3 post topic id 3 well! 3 1 topic 1 better topic 1.
my expected result
i getting nothing in return, though trying this:
1. random name example 2. filler topic name 3. exciting posts excellence
you can use left join without involving sub select
select t.* `topics` t left join `replies` r on t.id = r.tid group t.id order count(r.tid) desc
Comments
Post a Comment