1
votes

I have the following query. Could you please tell me how to implement it in codeigniter.

SELECT mnu_name,mnu_desc,mnu_type FROM central_web_auth_mnu 
  WHERE mnu_name NOT IN(
      SELECT mnu_name FROM central_web_auth_grp_mnu WHERE  gr_id='.$grp.'
  ) ORDER BY mnu_desc;

Thank You.

3
If your CI version is 2 you can follow the answers. - Shaiful Islam

3 Answers

4
votes
 $this->db->select('mnu_name, mnu_desc, mnu_type')->where('`mnu_name` NOT IN (SELECT `mnu_name` FROM `central_web_auth_grp_mnu` WHERE idgr_id= $grp)', NULL, FALSE)->order_by('mnu_desc', 'desc')->get('central_web_auth_mnu');

set false as third parameter so that escaping will skip:

3
votes

CI active db library does support array params in where_in() or where_not_in() methods, so you can use where() method and pass condition as string, try this query

$this->db->select('mnu_name, mnu_desc, mnu_type')
->where('mnu_name NOT IN(SELECT mnu_name FROM central_web_auth_grp_mnu WHERE gr_id='.$grp.')')
->order_by('mnu_desc', 'desc')
->get('central_web_auth_mnu')
1
votes

You can simply use codeigniter's Active Record library to achieve this (thanks Girish)

$this->db->select('mnu_name, mnu_desc, mnu_type')
->where('mnu_name NOT IN(SELECT mnu_name FROM central_web_auth_grp_mnu WHERE gr_id='.$grp.')')
->order_by('mnu_desc', 'desc');

$query = $this->db->get();
$num_rows = $query->num_rows();