Hello!
I have two tables, USERS and PHOTOS, in a one to many relationship like this...
USERS:
UserID: Name:
1 Bob
2 Frank
3 Jim
4 Bill
5 Keith
PHOTOS:
PhotoID: ImagePath: OwningUserID:
A test.jpg 1
B test2.jpg 1
C test3.jpg 2
D test4.jpg 2
E test5.jpg 3
So, here we see:
- Bob has 2 photos, test.jpg and test2.jpg
- Frank has 2 photos, test3.jpg and test4.jpg
- Jim has one photo, test5.jpg
- Bill and Keith have no photos
What I need is an SQL query that returns this recordset:
QUERYRESULT:
Name: NumberOfPhotos:
Bob 2
Frank 2
Jim 1
Bill 0
Keith 0
Can anybody help?
Thanks,
Al
LoopyNZ - 09 Sep 2004 13:21 GMT
Try this, Al, and let me know how it goes:
SELECT USERS.Name, Count(PHOTOS.PhotoID) AS NumberOfPhotos
FROM USERS LEFT JOIN PHOTOS ON USERS.UserID = PHOTOS.OwningUserID
GROUP BY USERS.Name
ORDER BY Count(PHOTOS.PhotoID) DESC;
------------
LoopyNZ
------------
Al Findlay - 10 Sep 2004 09:59 GMT
Hello!
Perfect. Thank you very much.
Al
> Try this, Al, and let me know how it goes:
>
> SELECT USERS.Name, Count(PHOTOS.PhotoID) AS NumberOfPhotos
> FROM USERS LEFT JOIN PHOTOS ON USERS.UserID = PHOTOS.OwningUserID
> GROUP BY USERS.Name
> ORDER BY Count(PHOTOS.PhotoID) DESC;