【问题标题】:codeigniter active records do not return expected resultcodeigniter 活动记录不返回预期结果
【发布时间】:2015-03-14 03:14:00
【问题描述】:

我有三张桌子

+----------+ +-------------+
| 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();

【问题讨论】:

    标签: php mysql codeigniter activerecord


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多