user1409671
user1409671

Reputation: 1

Getting error '#1111 - Invalid use of group function'

I am getting this error with following query. Please help me correct it.

#1111 - Invalid use of group function

All tables have personpkid as common field.

SELECT
    p.*,c.*,t.*
FROM
    t_person p
    LEFT JOIN tr_category c ON p.personpkid=c.personpkid
    LEFT JOIN tr_testimonial t ON p.personpkid=t.personpkid
WHERE
    avg(t.ratingvalue)>=5
GROUP BY
    p.personpkid ORDER BY approvedate DESC

Thank you in advance, SG

Upvotes: 0

Views: 1835

Answers (1)

Aleks G
Aleks G

Reputation: 57316

When you're using a grouping expression to filter results, you need to use HAVING clause. Try this:

SELECT
    p.*,c.*,t.*
FROM
    t_person p
    LEFT JOIN tr_category c ON p.personpkid=c.personpkid
    LEFT JOIN tr_testimonial t ON p.personpkid=t.personpkid
GROUP BY
    p.personpkid ORDER BY approvedate DESC
HAVING
    avg(t.ratingvalue)>=5

Upvotes: 3

Related Questions