tags:

views:

29

answers:

2

This time my setup looks like this: one table with galleries names

CREATE TABLE GalleriesName (
    gallery_id
    , gallery_name
) 

and another table with galleries photos

CREATE TABLE GalleriesPhotos (
    photo_id
    , photo_gallery_id
    , photo_name
) 

What I need is to get all the galleries with one random picture for each gallery.

Is it possible to do this with a single query?

+1  A: 

It depends how you have setup your tables in terms of relationship of primary key and foreign key. As for random rows, you need to append order by rand() to your query.

Sarfraz
+3  A: 

Is it possible to do this with a single query?

Yes it possible. you need to learn to make SUBSELECTS with mysql. Just select a gallery then subselect a random image [ with rand() ] with that gallery ID =)

http://www.fluffycat.com/SQL/Subselect/

holms
Great! Thank you!
Psyche