Codeigniter activerecords

时间:2014-01-30 14:05:23

标签: php codeigniter

我是codeigniter的新手。

我试图发送此查询:

    SELECT * FROM employees
    INNER JOIN authorization ON authorization.Employee_ID=employees.Employee_ID
    WHERE authorization.role='Team Leader'
    AND authorization.Employee_ID NOT IN(SELECT Employee_ID FROM team_leaders)

此查询用于选择具有团队领导职位(角色)但未分配给团队的员工。

我在Codeigniter的活动记录中执行此查询时遇到问题

我的错误型号代码:

function teamleaders(){
    $this->db->select('Employee_ID');
    $this->db->from('team_leaders');
    $query=$this->db->get();
    $teamleaderid=$query->result_array();
        //get teamleaders id who are assigned in a tam

    $this->db->select('employees.Employee_ID,employees.First_Name,employees.Last_Name');
    $this->db->from('employees');
    $this->db->join('authorization','authorization.Employee_ID=employees.Employee_ID');
    $this->db->where('authorization.role','Team Leader');
    $this->db->where_not_in('authorization.Employee_ID',$teamleadersid);
    $query=$this->db->get();

    return $query->result_array();
        }

请帮助我使用活动记录来做到这一点。

我非常感谢任何帮助

3 个答案:

答案 0 :(得分:1)

是的,你可以尝试,

   $this->db->select('employees.*');
   $this->db->from('employees');
   $this->db->join('authorization','authorization.Employee_ID=employees.Employee_ID');
   $this->db->where('authorization.role','Team Leader');
   $this->db->where('authorization.Employee_ID NOT IN (SELECT Employee_ID FROM team_leaders)');
   $query=$this->db->get();

答案 1 :(得分:0)

这个怎么样:

$sql="SELECT * FROM employees
INNER JOIN authorization ON authorization.Employee_ID=employees.Employee_ID
WHERE authorization.role='Team Leader'
AND authorization.Employee_ID NOT IN(SELECT Employee_ID FROM team_leaders)";

return $this->db->query($sql)->result_array();

答案 2 :(得分:0)

您的$teamleaderid数组不是您认为的格式。它是一个数组数组,而不是ID数组。

试试这个:

$teamleaderid = array_map(function($a){
    return $a['Employee_ID'];
}, $teamleaderid);