Codeigniter join table alias


Codeigniter join table alias – Codeigniter Database Class is used to join tables in coddiegniter. $this->db->join(); is used to join tables in codeigniter. It is very simple to join tables using alias name. Here in this tutorial, we are going to explain how you can use table alias to join tables in CI.


Codeigniter join table alias

Codeigniter join table alias

Here is simple example of join tables using alias name. We have taken alias name for users table as u, profile_image as pi, city as c, post as p and friends as f.


$this->db->select('u.id,u.name,pi.image,p.post,f.id as fid');
$this->db->from('users as u');
$this->db->join('profile_image as pi ', 'pi.user_id = u.id');
$this->db->join('city as c', 'c.user_id = u.id','left');
$this->db->join('post as p ', 'p.user_id = u.id','left');
$this->db->join('friends as f ', 'f.user_id = u.id','left');
$this->db->where('u.id', $id); 
$query = $this->db->get();

The above example will produce output something like this-
// Select u.id,u.name,pi.image,p.post,f.id as fid from users
join profile_image as pi on profile_image.user_id = users.id
left join city as c on c.user_id = u.id
left join post as p on p.user_id = u.id
left join friends as f on f.user_id = u.id
where u.id = $id


Advertisements

Add Comment

📖 Read More