Welcome Guest, Not a member yet? Register   Sign In
Active Record - Join a table twice
#1

[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?
#2

[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.




Theme © iAndrew 2016 - Forum software by © MyBB