Codeigniter active record left join issue Codeigniter active record left join issue codeigniter codeigniter

Codeigniter active record left join issue


You could alias them:

$this->db->select('accounts_transatctions.*, account_transactions.id AS a_id,                   accounts_bills_transactions.*,                    account_bills_transactions.id AS ab_id');$this->db->from('accounts_transactions');$this->db->join('accounts_bills_transactions', 'accounts_transactions.id = accounts_transactions.transaction_id','left');$query = $this->db->get();

The two IDs will now be available as a_id and ab_id (or whatever alias you choose)

Note: I'm not sure if you can alias in AR without avoiding escaping (haven't been using CI for a while). Should you get any error for that reason, just pass false as second parameter of $this->db->select():

$this->db->select('...', false);


you can try this if you confuse of using $this->where or $this->join

$query = $this->db->query("select ......"); return $query;


You problem is so simple. You can use this query

$query  =   $this->db                ->select('at.*')                ->select('abt.id as abt_id');                ->from('accounts_transactions at');                ->join('accounts_bills_transactions abt', 'at.id = abt.transaction_id','left');                ->get()                ->result();

When same column are used in join it selects only one. You need to give alise to the other column in second table. The best practice is to use a structure like this

accounts_transatctions--------------------------accounts_transatctions_idother_columnsaccounts_bills_transactions---------------------------accounts_bills_transactions_idaccounts_transatctions_idother_columns