You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ASC ORDER BY p.sort ASC, p.id ASC' at line 6SQL: SELECT pi.path FROM post AS p LEFT JOIN post_image AS pi ON ( pi.post_id = p.id ) WHERE p.category_id = 84 AND p.sort = 0 GROUP BY p.id ASC ORDER BY p.sort ASC, p.id ASC