Sub Query in CodeIgniter

Few month ago, i posted about sub query in codeigniter here. And today i’ve found another way. It using active record class in codeigniter. For example:

$this->db->select('(SELECT SUM(payments.amount) FROM payments WHERE payments.invoice_id=4') AS amount_paid', FALSE);
$query = $this->db->get('mytable');

Just put ‘FALSE’ to second parameter in select() function. $this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.

So simple. Isn’t it?:D

2 thoughts on “Sub Query in CodeIgniter

  1. Pingback: Sub Query dengan CodeIgniter - deptz personal blog

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.