Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining 2 tables... 1

Status
Not open for further replies.

manderson2006

Programmer
Feb 19, 2006
1
US
Here's my scenario:

I have two tables: PhotoAlbums (Album_ID, Album_Name) and Photos (Photo_ID, Photo_Name, Album_ID).

What I want to do is to display all of the Photo Albums with one randomly chosen photo from each Photo Album.

What SQL should I use to do this? Here's what I have right now:

SELECT A.Album_Name, P.Photo_Name FROM PhotoAlbums A, Photos P WHERE A.Album_ID = P.Album_ID

Obviously this doesn't work because every photo for every album will be displayed, instead of just one photo for each album.

Can someone help me here?

Thanks!
 
Try this

Select
a.Album_Name, p.Photo_Name
from PhotoAlbums A join Photos P on a.Album_ID = p.Album_ID

Where P.Album_ID = <Enter your Album_ID>

Hope it helps
 
How about this quick and dirty solution ? Its not random but providing your photo name is unique withn each album it should work :)

SELECT A.Album_Name, max(P.Photo_Name) FROM PhotoAlbums A,
Photos P WHERE A.Album_ID = P.Album_ID group by A.Album_Name
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top