2015-01-15 60 views
1

我有三個表爲笨的活動記錄不返回預期的結果

+----------+ +-------------+ 
| adv  | | removed_adv | 
+==========+ +=============+ 
| id  | | id   | 
| group | | adv_id  | 
| category | | member_id | 
| title | +-------------+ 
| path  | 
| duration | 
+----------+ 

我想從進階表中的數據對於一個給定組和類別。但是如果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

這是我的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();