select an.* from n_animals as an left join `n_numbers` as `num` on `an`.`Animal_Id` = `num`.`Animal_Id` left join `n_images` as `img` on `an`.`Animal_Id` = `img`.`Animal_Id` left join `jz_enkan` as `ekn` on `num`.`Enkan_Id` = `ekn`.`enkancode` where num.Enkan_Id is not null and (num.Males + num.Females + num.Unsexed) > ? and ekn.memberflag = ? group by an.Animal_Id