在codeigniter中使用WHERE NOT IN CLAUSE

时间:2015-04-08 06:38:53

标签: codeigniter

我有以下查询。你能告诉我如何在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;

谢谢。

3 个答案:

答案 0 :(得分:4)

 $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');

将false设置为第三个参数,以便转义将跳过:

答案 1 :(得分:2)

CI活动数据库库支持where_in()where_not_in()方法中的数组参数,因此您可以使用where()方法并将条件作为字符串传递,请尝试此查询

$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')

答案 2 :(得分:1)

你可以简单地使用codeigniter的Active Record库来实现这一点(感谢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();