Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用Codeigniter从同一个表进行多个SQL联接_Mysql_Codeigniter_Activerecord - Fatal编程技术网

Mysql 使用Codeigniter从同一个表进行多个SQL联接

Mysql 使用Codeigniter从同一个表进行多个SQL联接,mysql,codeigniter,activerecord,Mysql,Codeigniter,Activerecord,我有一个DB,其中有两列ID引用同一个表 如何对同一个表进行两次连接,并能够提取数据 以下是我所拥有的: $this->db->select('s.id, c.title, c.description, s.time ,s.day,s.instructor_change,s.studio_change,s.time_change,s.new_class,s.reservation_req,s.easy_does_it,s.mind,s.level,s.duration,s.locat

我有一个DB,其中有两列ID引用同一个表

如何对同一个表进行两次连接,并能够提取数据

以下是我所拥有的:

$this->db->select('s.id, c.title, c.description, s.time ,s.day,s.instructor_change,s.studio_change,s.time_change,s.new_class,s.reservation_req,s.easy_does_it,s.mind,s.level,s.duration,s.location,i.first,i.last');
            $this->db->from('schedule as s');
            $this->db->join('instructors as i', 'i.id = s.instructor_id','inner');
            $this->db->join('classes as c', 'c.id = s.class_id');
            $this->db->where('s.active', '1');
            $this->db->where('s.day', $dayofweek);
            $this->db->order_by('s.time',"ASC");

            $query = $this->db->get();
我也需要这样做 $this->db->join('i.id=s.'u alt_讲师_id

如何将同一个表连接两次,但在本例中,如何为同一条记录上的不同ID再次提取名称和姓氏

回答

$this->db->select('s.id, c.title, c.description, s.time ,s.day,s.instructor_change,s.studio_change,s.time_change,s.new_class,s.reservation_req,s.easy_does_it,s.mind,s.level,s.duration,s.location,i.first,i.last,a.first as alt_first,a.last as alt_last');
            $this->db->from('schedule as s');
            $this->db->join('instructors as i', 'i.id = s.instructor_id','left');
            $this->db->join('instructors as a', 'a.id = s.alt_instructor_id','left');
            $this->db->join('classes as c', 'c.id = s.class_id');
            $this->db->where('s.active', '1');
            $this->db->where('s.day', $dayofweek);
            $this->db->order_by('s.time',"ASC");

            $query = $this->db->get();

只是用不同的别名:

$this->db->join('instructors as ialt', 'ialt.id = s._alt_instructor_id');

调用它时,我使用的是左连接,因为这个ID可能为空。当我把它放到SELECT中,当我尝试给它取别名时,我在别名上得到了一个未定义的索引。我在您的帮助下得到了它,我在SELECT语句中有一个逗号
$this->db->select('*');
    $this->db->from('jobs');
    $this->db->join('company','company.cmp_name = jobs.cmp_name');
    $this->db->where('company.location', $data['location']);**strong text**