没有结果在Codeigniter中加入3个表

时间:2017-01-17 16:42:39

标签: php mysql codeigniter

即使我在数据库中有数据也没有结果:

$this->db->select('*');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID'); 
$this->db->join('classroom', 'user.classroomID = classroom.classroomID'); 
$this->db->where('roleID',"4");
$query = $this->db->get();
return $query->result();

2 个答案:

答案 0 :(得分:0)

在加入时,你必须用where condition定义表名。就像这个......

        $this->db->select('*');
        $this->db->from('user');
        $this->db->join('userprofile', 'user.userID = userprofile.userID'); 
        $this->db->join('classroom', 'user.classroomID = classroom.classroomID'); 
        $this->db->where('table_name.roleID',4);//table_name is name of table having column roleID
        $query = $this->db->get();
        return $query->result();

答案 1 :(得分:0)

$this->db->select('*');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID'); 
$this->db->join('classroom', 'user.classroomID = classroom.classroomID'); 
$this->db->where('roleID',"4");
$query = $this->db->get();
return $query->result();

your query work with one table data when you join another table you can must define $this->db->where('user.roleID',4) like this.

you can write this query like this for when you get the join table data 

$this->db->select('user.*,userprofile.youdesirename,classroom.class name');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID'); 
$this->db->join('classroom', 'user.classroomID = classroom.classroomID'); 
$this->db->where('user.roleID',4)
$query = $this->db->get();
return $query->result();