[thelist] sql query - how to evenly distributed results

r937 rudy at r937.com
Sun Feb 25 21:04:29 CST 2007


latest 4 images per gallery --

select a.gallery_id
     , b1.image_id
     , b1.image_date_added
  from galleries as a
inner
  join images as b1
    on a.gallery_id = b1.image_gallery_id
inner
  join images as b2
    on a.gallery_id = b2.image_gallery_id
   and b1.image_date_added <= b2.image_date_added
 where galleries.gallery_id in ( 1, 2 )
group
    by a.gallery_id
     , b1.image_id
     , b1.image_date_added
having count(*) <= 4  
order
    by a.gallery_id
     , b1.image_date_added desc

rudy
http://r937.com/





More information about the thelist mailing list