在where子句中设置一个数组

时间:2014-04-10 07:11:02

标签: mysql sql codeigniter

我想在where子句中设置一个数组。

function rapport_detail_opbrengsten($idKlant){
            $this->db->from('Project');
                $this->db->join('Opbrengsten', 'Opbrengsten.idProject = Project.idProject');
if ($idKlant > 0){
        $this->db->where('idKlant', $idKlant);}
         $query = $this->db->get();
         $project = array();
            foreach($query->result() as $row){
                $project[] = $row->idProject;
            }
            return implode("`,`", $project);
                $this->db->select('idProject, SUM(Prijs) as total'); 
                $this->db->from('Opbrengsten');
                $this->db->where_in('idProject', $project);
                $this->db->group_by('idProject');
                $query = $this->db->get();

        if($query->num_rows()>0){
            return $query->result();
         }
        else{
             return false;
         }
     }

该回复提供以下内容:string(66) "7141481818114 9151112666,`6"

但不知何故,这在where_in或where子句中不起作用。

1 个答案:

答案 0 :(得分:0)

你错过了使用反引号,你需要引号。将你的where_in()改为

$this->db->where_in("idProject",$project);
/* $project should be an array containing list of ids not a string */

根据where_in()中的Active Record,您可以在第二个参数中传递值数组

或者只使用where()

$this->db->where("idProject IN('".join("','",$project)."')",FALSE);