phpmysqlcodeigniteractiverecordsubquery

Convert SQL containing a subquery in the FROM clause into CodeIgniter's active record syntax


I have a query

SELECT
    l.id,l.name,l.city,l.email,l.phone,l.status,l.source,l.leadCreatedTime,l.organizationId
FROM (
    SELECT  id
    FROM    leads
    where removed='0'
    ORDER BY id
    LIMIT 40000
) o
JOIN leads l
    ON l.id = o.id
ORDER BY
    l.id

I want to convert this query in Codeigniter format for eg

$this->db->select('l.id,l.name,l.city,l.email,l.phone,l.status,l.source,l.leadCreatedTime,l.organizationId');
$this->db->from('leads');

I'm having problems in the 8th line of query where the fetched data is defined as o. How am I suppose to write that in CI?


Solution

  • can u test my code it may help you as it is codegniter format

    $this->db- >select('l.id,l.name,l.city,l.email,l.phone,l.status,l.source,l.leadCreatedTime,l.organizationId');
    $this->db->from('(SELECT id FROM leads where removed="0" ORDER BY id LIMIT 40000) o');
    $this->db->join('leads l', 'l.id = o.id');
    $this->db->order_by('l.id');
    $result=$this->db->get();
    $show=$result->result_array();
    

    it is not in fully codeigniter format in sub query but it help you.

    Enjoy code.