codeigniter join 2 table data

It doesn't matter what table is first... Simply:

<?php

$this->db->select('t1.name, t2.something, t3.another')
     ->from('table1 as t1')
     ->where('t1.id', $id)
     ->join('table2 as t2', 't1.id = t2.id', 'LEFT')
     ->join('table3 as t3', 't1.id = t3.id', 'LEFT')
     ->get();
?>

Hi this will work for joining two tables in codeIgnator.

$this->db->select("chat.id,chat.name,chat.email,chat.phone,chat.date,post.post");
      $this->db->from('chat');
      $this->db->join('post', 'chat.id = post.id');
      $query = $this->db->get();

    if($query->num_rows() != 0)
    {
        return $query->result();
    }
    else
    {
        return false;
    }

You can change to the query as you like do trail and error method to get your appropriate results.


here is how it works:

  1. suppose we have two tables namely student, library.

note: but remember that one of the column should match if you want to use where condition/ here we assume that both tables have std_id column

  1. Write the the select query as follows, in the brackets write what are all the things you want

note:write as shown below don't put quotes to each single one put it on whole at once.

*note: suppose we want name, phone_no. from student table and book_name form library table.*

      $this->db->select('name, phone_number, book_name');
  1. Now write the from query and write one of the table name(No rule)

      $this->db->from('student');
    
  2. Now join this with the another table with join query

      $this->db->join('library', 'students.std_id=library_std_id');
    
  3. Now write the where condition that like you want book name form library table where std id=1(in practical you need to fetch this id from view/database)

      $this->db->where('std_id', 1);
      $q= $this->db->get();
    

That's it it's done now you can print and check the result.


$this->db->join('comments', 'comments.id = blogs.id');

With this line you tell: search me inside comments all comments with id equal blogs.id.

Usually is something like that I think:

$this->db->join('comments', 'comments.blogs_id = blogs.id');

You have to insert into your table a field named blogs_id (int value unisgned) because blogs can have more comments. Isn't important the position of first or second value