You can use GROUP_CONCAT(expr) how ever it is not recommended because of character length restriction of default set to 1024 character but it can be increased
$sql="SELECT d.* ,
GROUP_CONCAT(s.title) specialities,
GROUP_CONCAT(a.title) appointments
FROM tbl_doctor d
JOIN tbl_doctor_specialty ds ON (d.id = ds.doctor_id)
JOIN tbl_specialty s ON (s.id = ds.speciality.id)
JOIN tbl_appointment a ON (a.doctor_id =d.id)
GROUP BY d.id";
$result=$this->db->query($sql);
return $result->result();