There has GOT to be an easier way to do this--doesn't there?
I just spent _two hours_ on this, so I don't know. Trying to get the id associated with the minimum sort order of each set of images for a given set of "stock number"s.
SELECT min(i1.id) as id, i1.stock_number, i1.sort FROM inv_image_groups i1, (SELECT max(sort) as sort, stock_number FROM inv_image_groups GROUP BY stock_number) as i2 WHERE i1.stock_number IN (SELECT distinct stock_number from inv_image_groups) AND i1.stock_number = i2.stock_number and i2.sort = i1.sort GROUP BY i1.stock_number;
Essentially, there can be any number of images associated with a given vehicle, each with a sort order so we know how to display them. When listing a bunch of vehicles, I want to pull each image with the lowest sort order for each vehicle, to just show that one thumbnail.