与Codeigniter相同的表中的多个SQL连接

时间:2011-08-05 02:24:24

标签: mysql codeigniter activerecord

我有一个带有2列ID的DB,它引用了同一个表

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

这就是我所拥有的:

$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('instructors as i','i.id = s._alt_instructor_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();

2 个答案:

答案 0 :(得分:6)

使用不同的别名:

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

答案 1 :(得分:0)

$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**