使用联接进行Radius感知标记搜索

I'm trying to write a tag based search which orders results by distance from a center lat long and also limits to within a radius. I dont know how to write the SQL that will join the necessary tables and filter by the radius stuff too.

I have written the first non-radius aware query using codeiginiters active record as shown in the commented code below, this generates the SQL shown in $sql.

public function search_subjects_tag_radius($string, $lat = null, $lon = null, $rad=null){   

        /*
        $this->db->where("subject_tags.string_en","$string");
        $this->db->from('subject_tags');
        $this->db->join('subject_to_tag', 'subject_to_tag.tag_id = subject_tags.id');
        $this->db->join('subject', 'subject.id = subject_to_tag.subject_id');
        $this->db->limit("10");
        $result = $this->db->get();
        return $result -> result_object();
        */

        $sql = "SELECT *
        FROM (`subject_tags`)
        JOIN `subject_to_tag` ON `subject_to_tag`.`tag_id` = `subject_tags`.`id`
        JOIN `subject` ON `subject`.`id` = `subject_to_tag`.`subject_id`
        WHERE `subject_tags`.`string_en` =  '%$string%'
        LIMIT 10
        ";


        $result = $this->db->query($sql)->result_object();

        return $result; 

        return $result;
    }

This is the working code i have used to do a radius search by title only (with no joins:

$sql2 = "SELECT *, ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( subject.long ) - radians($lon) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) )
        AS distance 
        FROM subject 
        WHERE `title` LIKE '%$string%' 
        OR `address` LIKE '%$string%'
        HAVING distance < 10 ORDER BY distance LIMIT 6";

Table relationship setup: Table relationship

I would like add the radius features from $sql2 to the query in $sql if you have any idea how to write that id love to know!

Thanks.