Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
ORDER BY number of occurrences in Database
Old 02-13-2007, 11:19 PM ORDER BY number of occurrences in Database
Skilled Talker

Posts: 64
Trades: 0
I have a forum and each post is saved in the database like so:
Table Name: forum_posts
| p_id | name | message | time | stat |
'p_id' is the Post ID and 'stat' is the post status, 0 is the status it should be.

Basically what I want to do is create a 'Top 10 Posters' section and a Top 10 Posters Today' section. This would require me to query the database based on how many times a username appeared in the 'name' column of my database and then gather how many times they posted. With 'Top 10 Today' the query would include a search certain periods of time.

The problem arises in the fact I have no idea how to order by the number of occurrences.
Could anyone tell me the query I'd need? If it helps, my site is PHP based if PHP is needed.
Petsmacker is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-14-2007, 02:11 AM Re: ORDER BY number of occurrences in Database
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
What you seem to be looking for is the Count function of SQL.

"Select User_ID, Count (P_ID) from Users inner join Posts on Users.User_ID = Posts.User_ID Group by User_ID order by Count (P_ID) DESC"

Count does exactly what it sounds like...it counts the number of occurrences of something.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 02-14-2007, 09:12 PM Re: ORDER BY number of occurrences in Database
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Adam is right, and you should give him talkupation for it.

Question for the A man, though. I've only ever used Count(*) and almost everybody I've worked with in SQL has also used Count(*). And when I read your reply suggesting Count(P_ID) I'm realizing I've never had a good reason to prefer Count(*), except maybe that it's faster to type and you could copy it into another sproc without changing a field name. But what's the actual difference between the two, or is there a difference?
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 02-15-2007, 12:10 AM Re: ORDER BY number of occurrences in Database
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
Well...using * in a query is noted for taking up more resource, since the query first has to figure out ALL of the fields in the query and then count them. (e.g. Select * is less efficient and takes up more resource than Select Field1, Field2, Field3).

So I always count by whatever my unique identifier field is for that table as an extension of that logic. Resource consumption and all that.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 03-04-2007, 04:13 PM Re: ORDER BY number of occurrences in Database
Super Talker

Posts: 144
Trades: 0
The difference between count(*) and count(field_name) -mainly in Oracle but I think the rule is for all- is that: Count(*) counts all rows provided by the Where clause including Nulls and Count(field_name) will count all the occurrences of the field_name which is not Null. So it all depends on what you want and if field_name is a required field and will never be Null, then you'll get the same result and you better use count(field_name) so the server won't have to combine all fields and count them - in count(*) - that's why count(*) takes more resources.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
AHelpingHand is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to ORDER BY number of occurrences in Database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.21612 seconds with 12 queries