Author Topic: mysql help  (Read 398 times)

0 Members and 1 Guest are viewing this topic.

Offline c_pattleTopic starter

  • Enthusiast
  • Posts: 284
    • View Profile
mysql help
« on: September 02, 2010, 06:27:09 AM »
I have the following sql which works fine


"SELECT AVG(ratings.score), articles.* FROM ratings, articles where ratings.article_number = articles.article_number group by article_number"


However I want to add to this statement to make sure that it only returns the results where the field avg(ratings.score) is between 3 and 4.  I tried the sql below but it came up with the error "invalid use of group function". 


SELECT AVG
(ratings.score), articles.* FROM ratingsarticles where ratings.article_number articles.article_number and AVG(ratings.score) < 4 group by article_number


Thanks for any help

Offline mikosiko

  • Devotee
  • Posts: 946
    • View Profile
Re: mysql help
« Reply #1 on: September 02, 2010, 06:35:36 AM »
article_number is ambiguous... you have that column in both tables... you must specify which one you want to use in your group by

error_reporting(E_ALL); 
ini_set("display_errors"1);

Offline c_pattleTopic starter

  • Enthusiast
  • Posts: 284
    • View Profile
Re: mysql help
« Reply #2 on: September 02, 2010, 06:41:17 AM »
Thanks.  I changed it to

SELECT AVG(ratings.score), articles.* FROM ratings, articles where ratings.article_number = articles.article_number and AVG(ratings.score) < 4 group by ratings.article_number;

However this is still give me to same problem.  Do you know what could be wrong?

Offline kickstart

  • Guru
  • Addict
  • *
  • Posts: 2,680
    • View Profile
Re: mysql help
« Reply #3 on: September 02, 2010, 07:35:26 AM »
Hi

You are trying to check against the result of an aggregate function, so check using HAVING rather than WHERE.

All the best

Keith
There are 10 types of people in the world. Those who understand binary and those who don't