You don't group by an aggregate, try removing CountOfName from your GROUP BY list.
Main Topics
Browse All TopicsHello,
When I run the query:
SELECT names.name, Count(names.name) AS CountOfname, photos.user, photos.censored FROM photos
RIGHT JOIN names ON photos.photo=names.photo
GROUP BY names.name, CountOfname, photos.user, photos.censored
HAVING ((photos.user='paul') AND (photos.censored=0))
ORDER BY Count(names.name) DESC
I receive the error: "Invalid use of group function"
Does anyone know what is happening?
Thanks,
Paul Robinson.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi Paul_F_Robinson,
this is better ...
put these sort of conditions in the where clause....
use the having clause for conditions testing the aggregate functions (count(.) etc)
SELECT names.name, Count(names.name) AS CountOfname, photos.user, photos.censored
FROM photos
RIGHT JOIN names
ON photos.photo=names.photo
Where photos.user='paul' AND photos.censored=0
GROUP BY names.name, photos.user, photos.censored
ORDER BY CountOfname DESC;
you could also substitute a 0 instead of photos.censored, and 'Paul' instead of the photos.user
and that would mean they could also come out of the group by clause.. which would reduce the amount
of work the database engine has to perform...
hth
Cheers!
as lowfatspread explained you HAVING clause only applies to aggregate functions. but if you use a condition (WHERE) on the left table in a RIGHT JOIN it will transform it in an INNER JOIN, so it should not provide the desired result.
there is something unclear in your query : do you have many names for on photo, or many photos for one name ?
Anyway, could you post few lines of your tables and explain describe the desired result in simple words (not SQL) ?
Hello,
The query i presented has had the desired effect when using MS Access. However I have now moved to MySQL.
Basically there is one photo record with many name records.
mysql> describe names;
+-------+-------------+---
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+---
| photo | varchar(50) | | MUL | | |
| name | varchar(50) | YES | | NULL | |
+-------+-------------+---
2 rows in set (0.00 sec)
mysql> describe photos;
+--------------------+----
| Field | Type | Null | Key | Default | Extra |
+--------------------+----
| photo | varchar(50) | | PRI | | |
| top_level_catagory | varchar(50) | YES | | NULL | |
| location | varchar(50) | YES | | NULL | |
| censored | tinyint(1) | YES | | NULL | |
| catagory | varchar(50) | YES | | NULL | |
| user | varchar(50) | | MUL | | |
| recycled | tinyint(1) | YES | | NULL | |
| date | date | YES | | NULL | |
+--------------------+----
8 rows in set (0.00 sec)
A photo would have a collection of people associated with it. I want to get a list of people with a number representing how many photos they are in.
Thanks for the help,
Paul.
Hello,
Thanks for all the help. I just copied and pasted lowFatSpread's sql into my code and it worked.
Squeebee: I tried removing countOfName from my origional sql, it madfe no difference.
I'm not sure why the sql works in MS Access but not in MySQL. I'd be interested to know why? (no points for that tho!)
Anyway, as LowFatSpread gave the correct solution, I have awarded hime the 500 points.
Thanks everyone for the help.
Paul.
The REAL reason that your code works (I just figured it out) is the ORDER BY. The error says Error in GROUP BY clause, but it is really the Order By that is causing you problems. If you notice the original code:
ORDER BY Count(names.name) DESC
and the replacement code:
ORDER BY CountOfname DESC;
The reason this works is because mySQL doesn't appear to support aggregate functions in the ORDER BY clause, and therefore you must declare an alias to the aggregate column in your select list.
Too bad you already gave out the points :).
Cameron
Business Accounts
Answer for Membership
by: VGRPosted on 2003-10-07 at 13:43:37ID: 9509027
try this :
SELECT names.name, Count(names.name) AS CountOfname, photos.user, photos.censored FROM photos
RIGHT JOIN names ON photos.photo=names.photo
GROUP BY names.name, CountOfname, photos.user, photos.censored
HAVING ((photos.user='paul') AND (photos.censored=0))
ORDER BY CountOfname DESC;