How To Write Subquery In Codeigniter Active Record

Request for callback

Details

CodeIgniter Active Records do not currently support sub-queries, However I use the following approach:

 

$this->db->select('id');
$this->db->from('table');
$where_clause = $this->db->_compile_select(false,true);
$this->db->_reset_select();

#Create main query

$this->db->select('*');

$this->db->from('table1');

$this->db->where("`id` NOT IN ($where_clause)", NULL, FALSE);

return $this->db->get('',null,null,true);

 

_compile_select() and _reset_select() are two undocumented (AFAIK) methods which compile the query and return the sql (without running it) and reset the query.

On the main query the FALSE in the where clause tells codeigniter not to escape the query (or add backticks etc) which would mess up the query. (The NULL is simply because the where clause has an optional second parameter we are not using)

However you should be aware as _compile_select() and _reset_select() are not documented methods it is possible that there functionality (or existence) could change in future releases.

The functions _compile_select() and _reset_select() are deprecated.
Instead use get_compiled_select():

 

Related Updates

Payment Gateway Integration Into A Website

Payment Gateway Integration Into A Website

In the online business creation process the first ideas are related to the business type, what to do, what to sell, and right there the question came out. How to charge for your services and/or produc ... Continue

Send Attachments With PHP Mail()?

Send Attachments With PHP Mail()?

To send an email with attachment we need to use the multipart/mixed MIME type that specifies that mixed types will be included in the email. Moreover, we want to use multipart/alternative MIME type to ... Continue

Send Email Using PHPMailer And Codeigniter

Send Email Using PHPMailer And Codeigniter

Send Email Using PHPMailer and CodeigniterCodeIgniter Email Class built for handling double-byte characters is really bad, cannot be properly set up Charest UTF-8.Here We want to share some informat ... Continue



Location

Shopali Building , Ghandhi ChowkDhrol-361210 Gujarat(India)
MoTuWeThFrSaSu
Open11:00 AM To 7:00 PM
Request For Quotation
Ique Infotech All rights reserved - Copyright © 2026
Powered by 4allBIZ