If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > How do I for a query to do this?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-08-07, 15:14
nickweavers nickweavers is offline
Registered User
 
Join Date: Jan 2005
Posts: 15
How do I for a query to do this?

I have a table containing people stuff and another table containing images of those people. One person can have up to four images. I would like to select all people who have at least one image. I only want one record row per person in the result.

How would I go about constructing a select to do this?

TIA,
Nick.
Reply With Quote
  #2 (permalink)  
Old 07-08-07, 16:10
nickweavers nickweavers is offline
Registered User
 
Join Date: Jan 2005
Posts: 15
Sorry to ask for help before I have properly researched but I didn't think I would locate a solution before it got answered here. I located a useful article which basically gave me the following solution:

SELECT profile.id,profile.name FROM AS p
WHERE EXISTS (SELECT * FROM images AS i WHERE p.id = i.user_id)

The article is here: http://dev.mysql.com/tech-resources/...ubqueries.html

Thanks,
Nick.
Reply With Quote
  #3 (permalink)  
Old 07-08-07, 16:46
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
SELECT DISTINCT profile.id,profile.name
FROM profile AS p INNER JOIN images AS i ON i.user_id = p.id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 07-08-07, 19:48
nickweavers nickweavers is offline
Registered User
 
Join Date: Jan 2005
Posts: 15
Thanks. That looks much simpler.
Reply With Quote
  #5 (permalink)  
Old 07-09-07, 03:59
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Possible alternative :
Code:
SELECT p.id
       , p.name
FROM profile p
WHERE (SELECT COUNT(*) FROM images WHERE user_id = p.id) >= 1
The advantage of the solution provided above is that you can now test for other numbers of images.
i.e. replace the 1 with 2 to get how many profiles have 2 or more images.
Or change the sign to = to get the profiles that have EXACTLY that number of images.
e.g. = 2

Last edited by aschk; 07-09-07 at 04:17.
Reply With Quote
  #6 (permalink)  
Old 07-09-07, 04:07
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
There may be some optimisation to be gained from reversing the where clause
i.e.
... WHERE 1 <= (SELECT COUNT(*) FROM images WHERE user_id = p.id)
Reply With Quote
  #7 (permalink)  
Old 07-09-07, 05:04
nickweavers nickweavers is offline
Registered User
 
Join Date: Jan 2005
Posts: 15
That looks very useful indeed. I have made a note of it.

Thank you.
Reply With Quote
  #8 (permalink)  
Old 07-09-07, 05:28
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
if efficiency is the criterion, my money's on the EXISTS solution
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 07-10-07, 03:44
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Yes quite possibly, however if you progress further and end up specifying other criteria for your select statement (in the WHERE clause) you might find that a dependent subquery will be more efficient, especially if your table has grown to a very large number of records. It's by the by really. All of the above solutions will do for what you are after.

n.b.
I'm a fan of the EXISTS or IN (non-dependent subquery) especially if it's for a small number of records. As rudy says, it's probably more efficient in this case.
Reply With Quote
  #10 (permalink)  
Old 07-10-07, 06:17
nickweavers nickweavers is offline
Registered User
 
Join Date: Jan 2005
Posts: 15
Thank you both for your kind help.

Nick.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On