如何在PHP中创建子查询?

So I am working on an existing system and I am trying to figure out how they have their mysql setup. They have a query setup like this

    $this->db->select('nID, nAID');
    $this->db->where('bValid', 1);
    $this->db->where('nID', (int)$nID);
    $this->db->from('event');
    $this->db->group_by('nID, nAID');

    $query = $this->db->get();

And this query works great, however I need to setup a sub query. I have written the query in SQL and tested it to confirm that it works but I am having trouble figuring out how to translate it. Basically what I need is rather than grabbing from 'event' table I need to grab from a sub selection I make prior to this. How I imagine it will be.

    $this->db->select('nID, nAID');
    $this->db->where('bValid', 1);
    $this->db->where('nID', (int)$nID);
    $this->db->from(
         $this->db->select('something, somethingelse');
         $this->db->where('something', 1);
         $this->db->get();  
    );
    $this->db->group_by('nID, nAID');

    $query = $this->db->get();

Step 1: Put this code into DB_active_rec.php

 // --------------------------------------------------------------------
    /**
    * Get SELECT query string
    *
    * Compiles a SELECT query string and returns the sql.
    *
    * @param    string    the table name to select from (optional)
    * @param    bool    TRUE: resets QB values; FALSE: leave QB vaules alone
    * @return    string
    */
   public function get_compiled_select($table = '', $reset = TRUE)
   {
       if ($table !== '')
       {
           $this->_track_aliases($table);
           $this->from($table);
       }

       $select = $this->_compile_select();

       if ($reset === TRUE)
       {
           $this->_reset_select();
       }

       return $select;
   }

Step 2: Try this

//Subquery
$this->db->select('something, somethingelse');
$this->db->where('something', 1);
$this->db->from('first_table');
$sub_query = $this->db->get_compiled_select();


$this->db->select('nID, nAID');
$this->db->where('bValid', 1);
$this->db->where('nID', (int)$nID);
$this->db->from("($sub_query) as tbl1");
$this->db->group_by('nID, nAID');

$query = $this->db->get();

Tell me if it works.

            $this->db->select('nID, nAID');
            $this->db->where('bValid', 1);
            $this->db->where('nID', (int)$nID);
            $this->db->from('event');
            $this->db->group_by('nID, nAID');

Translates to select nID, nAID from event where bValid=1 and nId='$nID' group by nID

your sub query

            $this->db->select('nID, nAID');
            $this->db->where('bValid', 1);
            $this->db->where('nID', (int)$nID);
            $this->db->from(
            $this->db->select('something, somethingelse');
            $this->db->where('something', 1);
            $this->db->get();  
            );
            $this->db->group_by('nID, nAID');

roughly translates to

            select nID, nAID select something,somethingelse where something=1 event where bValid=1 and nId='$nID' group by nID

I would suggest to run the sub query on every record in the loop