我有三张桌子
+----------+ +-------------+
| adv | | removed_adv |
+==========+ +=============+
| id | | id |
| group | | adv_id |
| category | | member_id |
| title | +-------------+
| path |
| duration |
+----------+
我想从特定组和类别的adv表中获取数据。但如果removed_adv.advid = adv.id,则结果集不应包含广告。我试过跟随。它返回没有filtering by $this->db->where_not_in('adv.id', 'ids from removed_adv');
$this->db->select('*');
$this->db->from('adv');
$this->db->where_not_in('adv.id', 'ids from removed_adv');
$this->db->where_in(array(
'category' => $cat_id,
'group' => $group_id
));
return $this->db->get();
答案 0 :(得分:0)
这是我的SELECT
声明:SELECT * FROM adv WHERE id NOT IN (SELECT adv_id FROM removed_adv) AND group IN (group_id1, group_id2,...) AND category IN (category1, category2,...)
所以你应该使用$this->db->last_query()
打印你的SQL语句并在SQL查询浏览器/ phpMyAdmin等上运行。
这是我的PHP代码
$query = $this->db->select('adv_id')->get('removed_adv');
$adv_id_array = $query->result_array();
$this->db->select('*');
$this->db->from('adv');
$this->db->where_not_in('adv.id', $adv_id_array);
$this->db->where_in(array(
'category' => $cat_id,
'group' => $group_id
));
return $this->db->get();