phpmysqlcodeigniteractiverecord

CodeIgniter Active Records where in subquery


How do I write where_in statement a subquery using codeigniter active records ?

$query = $this->db->query("SELECT SUM(a.transaction_payment_amount) FROM 
         transaction_payment a WHERE a.transaction_link IN 
         (SELECT transaction_link FROM transaction WHERE transaction_type = '22'");
$result = $query->result();

Now how to convert the above query into CI active records ?

I have tried:

$this->db->select("SUM(a.transaction_payment_amount)");
$this->db->from('transaction_payment a');
$this->db->where_in('a.transaction_link', "SELECT transaction_link from transaction WHERE transaction_type = '22'");
$query = $this->db->get();
$result = $query->result();

But it doesn't work.


Solution

  • If you use false then it will remove single quotes from where_in condition.

    $this->db->where_in('a.transaction_link', "SELECT transaction_link from transaction WHERE transaction_type = '22'", false);
    

    You can also use false in select,from like in every active record then it will remove single quote from active records.