SELECT DISTINCT resource_id FROM ( SELECT d.resource_id, d.created, d.downloads, @sum := if(@id = d.resource_id, @sum, 0) + d.downloads AS threshold, @id := resource_id as iid FROM view_cartoon_download_summary as d INNER JOIN dw_cci_search AS s ON (d.resource_id = s.image_id) CROSS JOIN (SELECT @id := '', @sum := 0) AS x WHERE s.image_id IS NOT NULL ORDER BY d.resource_id, d.created ) as a WHERE a.threshold = 19 ORDER BY created DESC LIMIT 4 OFFSET 0