subquery in codeigniter active record

Codeigniter

Codeigniter Problem Overview


SELECT * FROM certs WHERE id NOT IN (SELECT id_cer FROM revokace);

How do I write the above select statement in CodeIgniter active record?

Codeigniter Solutions


Solution 1 - Codeigniter

->where() support passing any string to it and it will use it in the query.

You can try using this:

$this->db->select('*')->from('certs');
$this->db->where('`id` NOT IN (SELECT `id_cer` FROM `revokace`)', NULL, FALSE);

The ,NULL,FALSE in the where() tells CodeIgniter not to escape the query, which may mess it up.

UPDATE: You can also check out the subquery library I wrote.

$this->db->select('*')->from('certs');
$sub = $this->subquery->start_subquery('where_in');
$sub->select('id_cer')->from('revokace');
$this->subquery->end_subquery('id', FALSE);

Solution 2 - Codeigniter

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

#Create where clause
$this->db->select('id_cer');
$this->db->from('revokace');
$where_clause = $this->db->get_compiled_select();

#Create main query
$this->db->select('*');
$this->db->from('certs');
$this->db->where("`id` NOT IN ($where_clause)", NULL, FALSE);

Solution 3 - Codeigniter

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

#Create where clause
$this->db->select('id_cer');
$this->db->from('revokace');
$where_clause = $this->db->_compile_select();
$this->db->_reset_select();

#Create main query
$this->db->select('*');
$this->db->from('certs');
$this->db->where("`id` NOT IN ($where_clause)", NULL, FALSE);

_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.

Solution 4 - Codeigniter

It may be a little late for the original question but for future queries this might help. Best way to achieve this is Get the result of the inner query to an array like this

$this->db->select('id');
$result = $this->db->get('your_table');
return  $result->result_array();

And then use than array in the following active record clause

$this->db->where_not_in('id_of_another_table', 'previously_returned_array');

Hope this helps

Solution 5 - Codeigniter

Like this in simple way .

    $this->db->select('*');
	$this->db->from('certs');
	$this->db->where('certs.id NOT IN (SELECT id_cer FROM revokace)');

    return $this->db->get()->result();

Solution 6 - Codeigniter

For query: SELECT * FROM (SELECT id, product FROM product) as product you can use:

$sub_query_from = '(SELECT id, product FROM product ) as product';
$this->db->select();
$this->db->from($sub_query_from);
$query = $this->db->get()

Please notice, that in sub_query_from string you must use spaces between ... product ) as...

Solution 7 - Codeigniter

I think this code will work. I dont know if this is acceptable query style in CI but it works perfectly in my previous problem. :)

$subquery = 'SELECT id_cer FROM revokace';

$this->db->select('*');
$this->db->where_not_in(id, $subquery);
$this->db->from('certs');
$query = $this->db->get();

Solution 8 - Codeigniter

$this->db->where('`id` IN (SELECT `someId` FROM `anotherTable` WHERE `someCondition`='condition')', NULL, FALSE);

Source : http://www.247techblog.com/use-write-sub-queries-codeigniter-active-records-condition-full-explaination/

Solution 9 - Codeigniter

    $where.= '(';
    $where.= 'admin_trek.trek='."%$search%".'  AND ';
    $where.= 'admin_trek.state_id='."$search".'  OR ';
	$where.= 'admin_trek.difficulty='."$search".' OR ';
	$where.= 'admin_trek.month='."$search".'  AND ';
	$where.= 'admin_trek.status = 1)';
            
    $this->db->select('*');
	$this->db->from('admin_trek');
	$this->db->join('admin_difficulty',admin_difficulty.difficulty_id = admin_trek.difficulty');
	$this->db->where($where); 
	$query = $this->db->get();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmardonView Question on Stackoverflow
Solution 1 - Codeignitergen_EricView Answer on Stackoverflow
Solution 2 - Codeigniteruser2336400View Answer on Stackoverflow
Solution 3 - CodeignitermattumotuView Answer on Stackoverflow
Solution 4 - CodeigniterAbhijit MazumderView Answer on Stackoverflow
Solution 5 - CodeigniterAbd AbughazalehView Answer on Stackoverflow
Solution 6 - Codeigniteruser3644751View Answer on Stackoverflow
Solution 7 - Codeigniterx3mCharlesView Answer on Stackoverflow
Solution 8 - CodeigniterMohit BumbView Answer on Stackoverflow
Solution 9 - CodeigniterShashank SaxenaView Answer on Stackoverflow