1

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?

2 Answers 2

3

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/

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.