phpcodeigniteractiverecordsubquerywhere-clause

Convert SQL with subquery in WHERE clause to CodeIgniter's active record


How can the following SQL be written via CodeIgniter active record methods?

SELECT p.*
FROM people p
WHERE p.age = (select max(subp.age) from people subp);

Solution

  • This will work

    $this->db->select('p.*');
    $this->db->from('people p');
    $this->db->where('p.age = (select max(subp.age) from people subp)',null,false);
    $result = $this->db->get()->result_array();
    print_r($result);