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 > query to retrieve all albums a photo is in

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-09-04, 19:12
one09jason one09jason is offline
Registered User
 
Join Date: Sep 2004
Posts: 1
query to retrieve all albums a photo is in

I could use a little help with a query for an online photo gallery system I built. I surmised from shopping around a little on the web that the way to create a photo gallery where photos can be organized into albums is to create 3 tables. Here is a simplified schema of what I have:

photos (id, title, caption)
albums (id, title)
albumphotos (id, photo_id, album_id)

This way, photos can belong to multiple albums, and I can easily retrieve all the photos in an album, etc. So far so good. Now, here comes my question:

How do I construct a query that will return each photo and all of the albums that it is a member of?

I'm thinking that I'd like a result that has columns like "photo title", "album1", "album2", "album3", etc. But I'd take the result any way I can get it. Please help if you can.

Thanks very much in advance...
Reply With Quote
  #2 (permalink)  
Old 09-09-04, 20:06
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
each photo and all of the albums that it is a member of:
Code:
select p.title    as phototitle
     , p.caption
     , a.title    as albumtitle
  from photos as p
inner
  join albumphotos as ap
    on p.id = ap.photo_id
inner
  join albums as a
    on ap.album_id
     = a.id 
order
    by p.title  
     , a.title
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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