Jump to content


Photo

Problem with short sql query


  • Please log in to reply
3 replies to this topic

#1 bwaxse

bwaxse
  • New Members
  • Pip
  • Newbie
  • 9 posts
  • LocationFort Worth Texas

Posted 25 September 2006 - 07:14 AM

Hello all,

I get "Invalid use of group function" with this query and don't know why.

I'm trying to look through a database of entries to find the user with the most entries and get the name and the number.  Thanks!

$sql = "SELECT sUsername, COUNT(sUsername) FROM tbl_questions WHERE sSchoolID = ".$_SESSION['schoolid']." ORDER BY COUNT(*) DESC LIMIT 0,1";

"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." -AE

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 25 September 2006 - 01:33 PM

Well, if you just need one user, you can use the MAX() function instead, with a GROUP BY.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 Gaoshan

Gaoshan
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 26 September 2006 - 02:03 AM

Do what fenway said.
Anyway, just for your information the reason you are having that problem is that you are mixing a GROUP column (in your case COUNT) with non-group columns and not using a GROUP BY clause.

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 26 September 2006 - 02:25 AM

You'd need a GROUP BY sUserName in any case.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users