Welcome to GraphicForumz.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Need help using SUM function in MySQL

 
   Graphic Forums (Home) -> SQL Integration RSS
Next:  CSS, Divs, and ap Divs.  
Author Message
AngryCloud

External


Since: May 20, 2007
Posts: 43



(Msg. 1) Posted: Thu May 22, 2008 8:02 am
Post subject: Need help using SUM function in MySQL
Archived from groups: macromedia>dreamweaver (more info?)

SELECT users.id, users.name, SUM(scores.points) AS user_score FROM users LEFT
JOIN scores ON users.id = scores.user_id WHERE user_score > 50

[b]How do I make this work?[/b]
(query should only return users with sums of points greater than 50)

 >> Stay informed about: Need help using SUM function in MySQL 
Back to top
Login to vote
Dooza

External


Since: Oct 02, 2007
Posts: 87



(Msg. 2) Posted: Thu May 22, 2008 2:11 pm
Post subject: Re: Need help using SUM function in MySQL [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

AngryCloud wrote:
> SELECT users.id, users.name, SUM(scores.points) AS user_score FROM users LEFT
> JOIN scores ON users.id = scores.user_id WHERE user_score > 50
>
> [b]How do I make this work?[/b]
> (query should only return users with sums of points greater than 50)
>

SELECT users.id, users.name, SUM(scores.points) AS user_score
FROM users LEFT JOIN scores ON users.id = scores.user_id
WHERE SUM(scores.points) > 50
GROUP BY users.id, users.name

 >> Stay informed about: Need help using SUM function in MySQL 
Back to top
Login to vote
Michael Fesser

External


Since: Sep 15, 2005
Posts: 733



(Msg. 3) Posted: Fri May 23, 2008 8:58 am
Post subject: Re: Need help using SUM function in MySQL [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

..oO(Dooza)

>AngryCloud wrote:
>> SELECT users.id, users.name, SUM(scores.points) AS user_score FROM users LEFT
>> JOIN scores ON users.id = scores.user_id WHERE user_score > 50
>>
>> [b]How do I make this work?[/b]
>> (query should only return users with sums of points greater than 50)
>>
>
>SELECT users.id, users.name, SUM(scores.points) AS user_score
>FROM users LEFT JOIN scores ON users.id = scores.user_id
>WHERE SUM(scores.points) > 50
>GROUP BY users.id, users.name

That's one way. The other would be a HAVING clause instead of WHERE,
which can also test dynamically created columns from the SELECT part:

SELECT ..., SUM(scores.points) AS user_score
FROM ...
HAVING user_score > 50

Micha
 >> Stay informed about: Need help using SUM function in MySQL 
Back to top
Login to vote
Dooza

External


Since: Oct 02, 2007
Posts: 87



(Msg. 4) Posted: Fri May 23, 2008 8:58 am
Post subject: Re: Need help using SUM function in MySQL [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Michael Fesser wrote:
> ..oO(Dooza)
>
>> AngryCloud wrote:
>>> SELECT users.id, users.name, SUM(scores.points) AS user_score FROM users LEFT
>>> JOIN scores ON users.id = scores.user_id WHERE user_score > 50
>>>
>>> [b]How do I make this work?[/b]
>>> (query should only return users with sums of points greater than 50)
>>>
>> SELECT users.id, users.name, SUM(scores.points) AS user_score
>>FROM users LEFT JOIN scores ON users.id = scores.user_id
>> WHERE SUM(scores.points) > 50
>> GROUP BY users.id, users.name
>
> That's one way. The other would be a HAVING clause instead of WHERE,
> which can also test dynamically created columns from the SELECT part:
>
> SELECT ..., SUM(scores.points) AS user_score
> FROM ...
> HAVING user_score > 50

Yup, saves time processing it too, as your not using the aggregate
function (SUM) twice.

Steve
 >> Stay informed about: Need help using SUM function in MySQL 
Back to top
Login to vote
AngryCloud

External


Since: May 20, 2007
Posts: 43



(Msg. 5) Posted: Fri May 23, 2008 10:03 am
Post subject: Re: Need help using SUM function in MySQL [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I am trying to work with a query structured like this:

SELECT users.id, users.name, SUM(scores.points) AS user_score FROM users LEFT
JOIN scores ON users.id = scores.user_id WHERE users.id = '$var' HAVING
user_score > 50


but I am getting this error:

Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is
illegal if there is no GROUP BY clause
 >> Stay informed about: Need help using SUM function in MySQL 
Back to top
Login to vote
Dooza

External


Since: Oct 02, 2007
Posts: 87



(Msg. 6) Posted: Fri May 23, 2008 2:08 pm
Post subject: Re: Need help using SUM function in MySQL [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

AngryCloud wrote:
> I am trying to work with a query structured like this:
>
> SELECT users.id, users.name, SUM(scores.points) AS user_score FROM users LEFT
> JOIN scores ON users.id = scores.user_id WHERE users.id = '$var' HAVING
> user_score > 50
>
>
> but I am getting this error:
>
> Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is
> illegal if there is no GROUP BY clause
>

Thats because you haven't grouped it.

Try this:

SELECT users.id, users.name, SUM(scores.points) AS user_score
FROM users LEFT JOIN scores ON users.id = scores.user_id
GROUP BY users.id, users.name
HAVING user_score > 50
 >> Stay informed about: Need help using SUM function in MySQL 
Back to top
Login to vote
Dooza

External


Since: Oct 02, 2007
Posts: 87



(Msg. 7) Posted: Fri May 23, 2008 2:13 pm
Post subject: Re: Need help using SUM function in MySQL [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Dooza wrote:
> AngryCloud wrote:
>> I am trying to work with a query structured like this:
>>
>> SELECT users.id, users.name, SUM(scores.points) AS user_score FROM
>> users LEFT JOIN scores ON users.id = scores.user_id WHERE users.id =
>> '$var' HAVING user_score > 50
>>
>>
>> but I am getting this error:
>>
>> Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP
>> columns is illegal if there is no GROUP BY clause
>>
>
> Thats because you haven't grouped it.
>
> Try this:
>
> SELECT users.id, users.name, SUM(scores.points) AS user_score
> FROM users LEFT JOIN scores ON users.id = scores.user_id
> GROUP BY users.id, users.name
> HAVING user_score > 50

Actually you want this:

SELECT users.id, users.name, SUM(scores.points) AS user_score
FROM users LEFT JOIN scores ON users.id = scores.user_id
WHERE users.id = '$var'
GROUP BY users.id, users.name
HAVING user_score > 50
 >> Stay informed about: Need help using SUM function in MySQL 
Back to top
Login to vote
bregent

External


Since: Nov 17, 2005
Posts: 365



(Msg. 8) Posted: Fri May 23, 2008 3:26 pm
Post subject: Re: Need help using SUM function in MySQL [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP
>columns is illegal if there is no GROUP BY clause

The error message is pretty clear. Anytime you have an aggregate in a select
statement, you need to include non-aggregate expressions in a group by clause.
Otherwise, it doesn't make any logical sense.
 >> Stay informed about: Need help using SUM function in MySQL 
Back to top
Login to vote
Display posts from previous:   
   Graphic Forums (Home) -> SQL Integration All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]