如何使用活动记录编写这样的子查询

时间:2015-10-24 13:19:57

标签: codeigniter

朋友们,我想在codeigniter中使用活动记录编写此查询,

public function getHallServices($city,$receiption_capacity,$booked_date){
      $sql="select hi.*,hf.* FROM hall_info hi, hall_feature hf WHERE 
            hf.hall_info_id=hi.id and hi.city=? and hf.receiption_capacity<=? and hi.id not IN
            (select hall_info_id FROM events
              WHERE event_date = ? 
              GROUP BY hall_info_id
            )";
      $query=$this->db->query($sql,array('city'=>$city,'receiption_capacity'=>$receiption_capacity,'event_date'=>$booked_date));
      return $query->result_array(); 
    }

此查询在codeigniter中未按预期工作,但在postresql中正常工作。我没有得到如何将参数传递给内部查询。请帮我纠正或者给出有效的记录查询。

1 个答案:

答案 0 :(得分:0)

将您的代码修改为:

public function getHallServices($city,$receiption_capacity,$booked_date){
  $sql="select hi.*,hf.* FROM hall_info hi, hall_feature hf WHERE 
        hf.hall_info_id=hi.id and hi.city=? and hf.receiption_capacity<=? and hi.id not IN
        (select hall_info_id FROM events
          WHERE event_date = ? 
          GROUP BY hall_info_id
        )";
  $query=$this->db->query($sql,array($city,$receiption_capacity,$booked_date));
  return $query->result_array(); 
}

告诉我它是否有效。