SQL query now uses LEFT JOIN when it should