MYSQL Query Problem: Select all threads, order by amount of no's who correspond to the thread
My title probably is a bit confusing, so here is my problem.
This is my table:
CREATE TABLE `b_posts` (
`thread` int(12) NOT NULL,
`no` int(12) NOT NULL,
`comment` text NOT NULL,
`gone` int(1) NOT NULL 开发者_开发百科default '0',
PRIMARY KEY (`no`),
FULLTEXT KEY `comment` (`comment`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
I now need a query which selects ALL threads which are not gone (means gone is 0, if done is 1 this would mean the thread has been deleted). The order of which the threads are selected should be by the amount of no's who have the same thread like the original.
Further explanation:
thread | no | comment | gone
100 | 100 | hello there, this is the thread! | 0
100 | 102 | this is a reply in the thread 100 | 0
100 | 103 | another reply in the same thread | 0
104 | 104 | this is a different thread | 0
104 | 105 | a reply to the different thread | 0
I now want to get the following data in the following order with my query:
thread | no | comment | gone
100 | 100 | hello there, this is the thread! | 0
104 | 104 | this is a different thread | 0
(Thread starter is defined when thread == no)
Since your qualifier of "thread = no" and "gone = 0", I saw no point in including them in the result columns... however, I did include the sub-count of total entries per thread
select b.thread,
b.comment,
postCounts.TotalEntries
from
b_posts b
join ( select b2.thread, count(*) as TotalEntries
from b_posts b2
group by b2.thread ) postCounts
on b.thread = postcounts.thread
where
b.thread = b.no
and b.gone = 0
order by
postCounts.TotalEntries DESC
精彩评论