Simple, Most Topic

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SheDesigns
Forum Commoner
Posts: 42
Joined: Tue Nov 18, 2008 9:51 am
Location: Buffalo, NY

Simple, Most Topic

Post by SheDesigns »

I'm having a brain fart! :x

I need to find what topics the user has the most entries in.

select msg_author, msg_topic from messages where msg_author = 'mary';

msg_author msg_topic
mary diet
mary diet
mary exercise
mary recipes
mary recipes
mary recipes
mary recipes
mary yoga
mary yoga

So obviously, Mary's most popular topic is "recipes".

1) How could I write a query that would pull her most used msg_topic?
2) If 2, or 3 msg_topics are equal, I would like all of the topics to display.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Simple, Most Topic

Post by pickle »

A query with COUNT() and GROUP BY() should do it.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Simple, Most Topic

Post by susrisha »

havent checked much or did any research but try this

Code: Select all

 
select topic, max(topic_count) from (Select count(msg_topic ) as topic_count,msg_topic as topic from messages where msg_author='mary');
 
Post Reply