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