I have a DB with 2 columns of ID that reference the same table
How do I do 2 joins to the same table, and be able to pull the data.
Here is what I have:
$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();
I need to also pull something like this $this->db->join('instructors as i', 'i.id = s._alt_instructor_id');
How do I join the same table twice, but be able to pull the in this case the first and last name again for a different ID on the same record?
Answer
$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();
Just alias it differently:
$this->db->join('instructors as ialt', 'ialt.id = s._alt_instructor_id');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With