DELETE posts
FROM posts pp
WHERE EXISTS(SELECT 1
FROM posts pm
WHERE pm.authorid = pp.authorid
AND pm.id < pp.id)
SELECT albums.id, albums.name, min(photos.thumb_url)
FROM `custom_app_bigd_gallery_albums` albums
LEFT JOIN `custom_app_bigd_gallery` photos ON albums.id=photos.album_id
WHERE photos.thumb_url IS NOT NULL
GROUP BY photos.album_id, albums.name
ORDER BY albums.id DESC
LIMIT 0, 25