Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter join 2 table data

hi everyone i am new to codeigniter and currently working on a small project in the project i am trying to join two tables and display there data in single table. i looked at the user guide that codeigniter has an i am not sure how this work

$this->db->join();

what table should be first and what id key should be firs. Can someone explain me more in detail about this please use examples if u can. I am trying to join credential table and tblanswers. Tnx for answering.

i have tried to code a function using this example:

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

$query = $this->db->get();

EDIT: instead of using join method in codeigniter is it possible to use a simple function to retrieve the two table data separately? all i want is to echo the data from database table on to a html table in my website page to be displayed is it possible to write two get functions to retrieve two tables separately ?

like image 338
Edvinas Liutvaitis Avatar asked Mar 04 '13 11:03

Edvinas Liutvaitis


People also ask

How do you JOIN a table in CI 4?

CodeIgniter 4 Query Builder join() method: inner This join() method produces the below MySQL query as returned by $db->getLastQuery(): MySQL default JOIN query. CodeIgniter 4 Query Builder join() method with the explicit inner keyword. MySQL INNER JOIN query.

How add JOIN in CodeIgniter?

$this->db->join() Multiple function calls can be made if you need several joins in one query. If you need a specific type of JOIN you can specify it via the third parameter of the function. Options are: left, right, outer, inner, left outer, right outer and full outer.

How use left JOIN in CI?

If you want to use left outer join OR left join, pass the third parameter in join() function/method using Codeigniter. $this->db->select('*') ->from('users') ->join('comments','comments. user_id = users. u_id','left')//this is the left join in codeigniter ->get();


4 Answers

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();
?>
like image 116
rafciokk0 Avatar answered Oct 24 '22 09:10

rafciokk0


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.

like image 32
Detroit Charan Avatar answered Oct 24 '22 09:10

Detroit Charan


$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

like image 1
Alessandro Minoccheri Avatar answered Oct 24 '22 09:10

Alessandro Minoccheri


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.

like image 1
Rajasekhar Avatar answered Oct 24 '22 08:10

Rajasekhar