title: How to join tables based on condition in codeigniter
body: <p>I want to write a query in such a way that,if received id matches with $id then request should join with profile table as </p>
  
  <pre><code>r.sent_id = profile_id
  </code></pre>
  
  <p>If sent_id matches with $id,then</p>
  
  <pre><code> r.sent_id = profile_id
  </code></pre>
  
  <p>My model,</p>
  
  <pre><code>public function getRequestedDetails($id) {
  $this-&gt;db-&gt;select('*');
  $this-&gt;db-&gt;from('profile');
  $stat =  $this-&gt;db-&gt;where('recieved_id', $id);
  if($stat)
  $this-&gt;db-&gt;join('requests enter code hereas r', 'r.sent_id = profile_id', 'left');
  $stat =  $this-&gt;db-&gt;where('sent_id', $id);
  if($stat)
  $this-&gt;db-&gt;join('requests as r', 'r.recieved_id = profile_id', 'left');
  $this-&gt;db-&gt;where('r.status', 1);
  $query = $this-&gt;db-&gt;get();
  $list = $query-&gt;result();
  return $list;
  }
  </code></pre>
  
  <p>I tried this,it shows the following error</p>
  
  <pre><code>Error Number: 1066
  </code></pre>
  
  <p>Not unique table/alias: 'r'</p>
  
  <pre><code>SELECT * FROM `profile` LEFT JOIN `requests` as `r` ON `r`.`sent_id` = `profile_id` LEFT JOIN `requests` as `r` ON `r`.`recieved_id` = `profile_id` WHERE `recieved_id` = '5' AND `sent_id` = '5' AND `sent_id` = '5' AND `r`.`status` = 1
  </code></pre>
  
  <p>Can anyone help me.Thanks.</p>