[thelist] single select image from each gallery - MySQL

r937 rudy at r937.com
Wed Jul 2 21:35:51 CDT 2008

> Group by seems to work fine, so quickly we forget...
> select gallery_id, image_id from gallery, images where
> gallery.gallery_id = images.image_gallery_id group by gallery_id

that works in mysql, but it will fail totally in any other database system,
simply because it's non-standard

mysql lets you put a column into the SELECT clause and leave it out of the
GROUP BY clause

it this column is not one-to-one with the GROUP BY column(s) -- and in this
case it isn't -- then the value will be indeterminate

read about it in the mysql docs under "GROUP BY and HAVING with hidden

that said...

... it is exactly appropriate for your needs

in this case, you get an indeterminate image_id for each gallery_id



More information about the thelist mailing list