CodeIgniter Forums
Active Record - Join a table twice - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Active Record - Join a table twice (/showthread.php?tid=51369)



Active Record - Join a table twice - El Forum - 05-01-2012

[eluser]brianatlarge[/eluser]
Basically I'm trying to get my customers contact information and salesmens contact information returned.

I have a customers table that references my contacts table twice. First for the customers contact information (customers.contact_id) and secondly for the salespersons contact information (customers.customer_salesman).

Code:
$this->db->select('*');
$this->db->from('customers');
$this->db->join('contacts', 'contacts.contact_id = customers.contact_id');
$this->db->join('contacts AS salesmen', 'salesmen.contact_id = customers.customer_salesman');

The problem is, my results only show the data from the contacts table for my last join. It won't return the information from when I joined on customers.contact_id. Is there any way to get it to return the results for customers.contact_id and customers.customer_salesman?


Active Record - Join a table twice - El Forum - 05-01-2012

[eluser]Stefan Hueg[/eluser]
As every contacts-table has the same column names (and they overwrite each other), you have to specify aliases, like this:

Code:
$this->db->select('customers.*, contacts.contact_id as c_contact_id, salesmen.contact_id as s_contact_id, ....);

And do this with every field.