Psyche
Psyche

Reputation: 8773

Problem with MySQL query

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?

Upvotes: 1

Views: 57

Answers (2)

holms
holms

Reputation: 9560

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/

Upvotes: 3

Sarfraz
Sarfraz

Reputation: 382636

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.

Upvotes: 1

Related Questions