473,480 Members | 4,939 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL count across two tables in a one to many relationship

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
Nov 13 '05 #1
2 11242
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
------------

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #2
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;

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2400
by: Robert Oschler | last post by:
I read a while back that MySQL will only use one index per query. (If this is not so, please tell me and point me to a doc that gives a good explanation of MySQL's current index usage policy). ...
2
2031
by: Marco Simone | last post by:
Hi, I have 4 tables, tblCompany, tblA, tblB and tblC. tblA, tblB and tblC contain same type of data, they should be in one table, but since there are many fields, I split it into 3 tables. Each...
2
1672
by: Tim Pollard | last post by:
Hi I'm hoping someone can help me with an access problem I just can't get my head around. I normally use access as a back end for asp pages, just to hold data in tables, so queries within access...
11
4501
by: dskillingstad | last post by:
I've been struggling with this problem for some time and have tried multiple solutions with no luck. Let me start with, I'm a novice at Access and I'm not looking for someones help to design my...
2
2206
by: The Frog | last post by:
Hello everyone, I am trying to find way of writing an SQL query that can produce missing record combinations across a many to many type setup in Access. The three tables used are as follows:...
1
1552
by: deshg | last post by:
Hey everyone, I think i am being kind of stupid but i can't quite work out how to do it and wondered if anyone might be able to help!? I'm using MS Access 2002 on Windows XP FYI. I have a...
2
2167
by: access baby | last post by:
I have 5 tables need to create relationship Customer Table CustomerID(auto number)(primary key) Customer Name(text) Customer City(text) Customer St(txt) Customer OrderTable Customer...
1
1013
by: robelind | last post by:
Hi. I'm having problems achieving the following: Three tables are involved: A: a_id, a_value B: b_id, a_id, b_value C: c_id, b_id, c_value Between A and B, there'a one-to-one relationship,...
6
4046
by: NicoleCartrette | last post by:
Going back to school is easier said than done.. This was posted to an older thread earlier but I don't think it got any attention. Your help is appreciated Professor requires we create a...
0
7041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6736
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6908
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4478
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
178
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.