use group by messages.category instead of categories.title
select c.id, c.title, count(m.category) as cnt from messages m
left join categories c
on c.id=m.category
group by m.category
order by cnt desc
acorn71 написав:
Quote:
Hello,
>
I need to write a SELECT statement that will display the most popular
categories.
>
This means I need a 'category count' for each of the messages in the
messages table, and I don't know how to do this.
>
Here is the structure of the 2 tables:
>
messages table
id, title, message, category, thread, status, date_posted
>
categories table
id, title, sub_cat
>
I've tried the following:
select categories.id, categories.title, count(messages.category) as
count from messages, categories group by categories.title;
>
but 'count' only returns the total count of messages with a category
entry, and I need the number of times each category id is entered in
the messages table.
>
Any help is greatly appreciated. Also, does anyone have any online
references for learning more complex SQL statements?
Thanks,
Aaron