SELECT SQL_CALC_FOUND_ROWS b.id AS file_id, a.id, a.name, IF(sessions.userid IS NOT NULL, '1','0') AS status FROM defaultusers a LEFT JOIN sessions ON a.id = sessions.userid INNER JOIN photos b ON b.id = (SELECT MAX(datestamp) FROM photos WHERE user_id = a.id) INNER JOIN defaultusers c ON c.id = 39212 WHERE a.status = 'N' and a.sex = 'w' and YEAR(CURDATE())-YEAR(a.birthday) <= 30 and YEAR(CURDATE())-YEAR(a.birthday) >= 16 and (ACOS((SIN(RADIANS(c.lat))*SIN(RADIANS(a.lat))) + (COS(RADIANS(c.lat))*COS(RADIANS(a.lat))*COS(RADIANS(a.lon)-RADIANS(c.lon)))) * 6378.388) <= 50 ORDER BY a.lastlogin DESC LIMIT 0,10;