iEntry 10th Anniversary Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read
Database Discussion Forum This is the place to find help resolving those nagging questions you have about implementing and using all kinds of databases. Need help writing a query? Need an opinion on Oracle? Post here!

Share Thread: & Tags

Share Thread:

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-11-2009, 03:27 PM
WebProWorld Member
 
Join Date: Aug 2003
Location: Jersey City, NJ
Posts: 74
jazzmatazz2005 RepRank 0
Default mysql_query

hi wpw

in the database there are multiple records for the same person.
for example

person 1 has 5 entries
person 2 has 3 entries
and so forth.

all im trying to do is get a simple count of who has the most entries from highes to lowest.

i wrote this query it works well for me but im using mysql version 5.1.33 locally.

SELECT mname, COUNT(mname) FROM votes GROUP BY mname ORDER BY COUNT(mname) DESC

the server this will run on uses a older version of mysql. im not sure which one though.

any ideas on how i can rewrite this?
__________________
It's better to do business with me than against me!
Reply With Quote
  #2 (permalink)  
Old 08-11-2009, 04:00 PM
WebProWorld New Member
 
Join Date: Aug 2009
Location: San Diego, CA
Posts: 6
dmartin RepRank 0
Default Re: mysql_query

That looks perfectly fine for whatever version of MySQL you need...certainly on MySQL 5.0, and 4.x...

Is there a reason you think it won't work? GROUP BY and COUNT are pretty standard stuff.

As far as the query itself, you might want to group by the person ID (if there is one), rather than the name (in case 2 people have the same name like "John Smith"), otherwise no problem.
Reply With Quote
  #3 (permalink)  
Old 08-11-2009, 04:36 PM
Uncle Dog's Avatar
WebProWorld Pro
 
Join Date: Apr 2008
Location: Scotland
Posts: 254
Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5Uncle Dog RepRank 5
Default Re: mysql_query

I'd agree with dmartin about compatibility (and ID). Have you actually tried (and failed) to run this on the live server yet or are you just assuming it will have problems?
Reply With Quote
  #4 (permalink)  
Old 08-11-2009, 04:45 PM
WebProWorld Member
 
Join Date: Aug 2003
Location: Jersey City, NJ
Posts: 74
jazzmatazz2005 RepRank 0
Default Re: mysql_query

Thanks Uncle Dog and dmartin.
i should have mentioned that detail.

I tried to run it.
The error i receive is:
Invalid use of group function

if i run it without ORDER BY COUNT(mname) DESC
it will output all the information but not in order.

fyi

the version of mysql that's live is 3.23.58
__________________
It's better to do business with me than against me!
Reply With Quote
  #5 (permalink)  
Old 08-11-2009, 04:59 PM
WebProWorld New Member
 
Join Date: Aug 2009
Location: San Diego, CA
Posts: 6
dmartin RepRank 0
Default Re: mysql_query

Hmmm....still seems valid, but try this:

SELECT mname, COUNT(mname) as numvotes FROM votes
GROUP BY mname
ORDER BY numvotes DESC

Maybe using an alias is needed in 3.23?
Reply With Quote
  #6 (permalink)  
Old 08-11-2009, 05:55 PM
WebProWorld Member
 
Join Date: Aug 2003
Location: Jersey City, NJ
Posts: 74
jazzmatazz2005 RepRank 0
Default Re: mysql_query

thanks dmartin

That worked out fine.

I dont know why my original way didn't work.

Thanks again
__________________
It's better to do business with me than against me!
Reply With Quote
  #7 (permalink)  
Old 08-11-2009, 06:03 PM
WebProWorld New Member
 
Join Date: Aug 2009
Location: San Diego, CA
Posts: 6
dmartin RepRank 0
Default Re: mysql_query

Glad that worked - old MySQL versions are a little quirky in terms of the SQL they support - my guess is that expressions without aliases don't work in 3.23 with a GROUP BY...who knows.

Maybe your installation can eventually upgrade to something better supported - 3.23 (and 4.0 and 4.1) have been "end of lifed".
Reply With Quote
  #8 (permalink)  
Old 08-12-2009, 09:53 AM
wige's Avatar
Moderator
WebProWorld Moderator
 
Join Date: Jun 2006
Location: United States
Posts: 2,648
wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9
Default Re: mysql_query

I believe this issue is that in older versions of MySQL you couldn't run a group function outside of the SELECT - in your case, COUNT. I think this was an optimization issue, the second count would require a new database call, whereas using an alias allows you to sort the results by the COUNT you already did in the SELECT statement.
__________________
The best way to learn anything, is to question everything.
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 08:28 PM.



Search Engine Optimization by vBSEO 3.3.0