views:

74

answers:

1

I am by no means fluent in MYSQL. What I am trying to do is I have a table that has a list of Galleries with unique ID numbers. I have another table ImageData that has images that may be as many as 1000 that are related to each GalleryID that is in the first table. I want to do a select that will get 5 random records from each GalleryID. I am trying to do this without doing a loop and multiple hits to the database.

The tables are as follows Galleries -> GalleryID -> GalleryName

ImageData -> ImageID -> ImageName -> GalleryID

I hope this makes sense.

+1  A: 

Hi Randy welcome to the club. Here is a simple and fast solution to your scenario. Enjoy

select g.GalleryName, i.imageName from Galleries g, imageData i, (SELECT gl.Galleryid from Galleries gl ORDER BY RAND() LIMIT 5) m where m.galleryID = i.galleryID

Mohsin Sheikh Khalid
That wouldn't work--a sub select can only return one row of data; you're trying to put upwards of five.
OMG Ponies