[php] How to INNER JOIN 3 tables using CodeIgniter

Can someone Tell me how to join 3 table with php? Example

SELECT FROM table1, table2,table on INNERJOIN -------------------

let I have 3 table.(question table ,answer table and category table) Here is example form my webpage.

Time remaining 30 minutes(I will get "30 minutes" form Category table)
1. Question (from question table)
2. answer (from answer table)

I don't know how to join 3 table.

This question is related to php mysql codeigniter

The answer is


$this->db->select('*');    
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id','JOIN Type');
$this->db->join('table3', 'table1.id = table3.id');
$query = $this->db->get();

this will give you result from table1,table2,table3 and you can use any type of join in the third variable of $this->db->join() function such as inner,left, right etc.


$this->db->select('*');    
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id', 'inner');
$this->db->join('table3', 'table1.id = table3.id', 'inner');
$this->db->where("table1", $id );
$query = $this->db->get();

Where you can specify which id should be viewed or select in specific table. You can also select which join portion either left, right, outer, inner, left outer, and right outer on the third parameter of join method.


function fetch_comments($ticket_id){
    $this->db->select('tbl_tickets_replies.comments, 
           tbl_users.username,tbl_roles.role_name');
    $this->db->where('tbl_tickets_replies.ticket_id',$ticket_id);
    $this->db->join('tbl_users','tbl_users.id = tbl_tickets_replies.user_id');
    $this->db->join('tbl_roles','tbl_roles.role_id=tbl_tickets_replies.role_id');
    return $this->db->get('tbl_tickets_replies');
}

I created a function to get an array with the values ??for the fields and to join. This goes in the model:

  public function GetDataWhereExtenseJoin($table,$fields,$data) {
    //pega os campos passados para o select
    foreach($fields as $coll => $value){
        $this->db->select($value);
    }
    //pega a tabela
    $this->db->from($table);
    //pega os campos do join
    foreach($data as $coll => $value){
        $this->db->join($coll, $value);
    }
    //obtem os valores
    $query = $this->db->get();
    //retorna o resultado
    return $query->result();

}

This goes in the controller:

$data_field = array(
        'NameProduct' => 'product.IdProduct',
        'IdProduct' => 'product.NameProduct',
        'NameCategory' => 'category.NameCategory',
        'IdCategory' => 'category.IdCategory'
        );
    $data_join = array
                    ( 'product' => 'product_category.IdProduct = product.IdProduct',
                      'category' => 'product_category.IdCategory = category.IdCategory',
                      'product' => 'product_category.IdProduct = product.IdProduct'
                    );
    $product_category = $this->mmain->GetDataWhereExtenseJoin('product_category', $data_field, $data_join);

result:

echo '<pre>';
    print_r($product_category);
    die;

you can modiv your coding like this

 $this->db->select('a.nik,b.nama,a.inv,c.cekin,c.cekout,a.tunai,a.nontunai,a.id');
 $this->db->select('DATEDIFF (c.cekout, c.cekin) as lama');
 $this->db->select('(DATEDIFF (c.cekout, c.cekin)*c.total) as tagihan');
 $this->db->from('bayar as a');
 $this->db->join('pelanggan as b', 'a.nik = b.nik');
 $this->db->join('pesankamar_h as c', 'a.inv = c.id');
 $this->db->where('a.user_id',$id);
 $query = $this->db->get();
 return $query->result();

i hope can be resolve your SQL


I think in CodeIgniter the best to use ActiveRecord as wrote above. One more thing: you can use method chaining in AR:

$this->db->select('*')->from('table1')->join('table2','table1.id=table2.id')->...

For executing pure SQL statements (I Don't Know About the FRAMEWORK- CodeIGNITER!!!) you can use SUB QUERY! The Syntax Would be as follows

SELECT t1.id FROM example t1 INNER JOIN (select id from (example2 t1 join example3 t2 on t1.id = t2.id)) as t2 ON t1.id = t2.id;

Hope you Get My Point!


I believe that using CodeIgniters active record framework that you would just use two join statements one after the other.
eg:

$this->db->select('*');
$this->db->from('table1');
$this->db->join('table1', 'table1.id = table2.id');
$this->db->join('table1', 'table1.id = table3.id');
$query = $this->db->get();

Give that a try and see how it goes.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to mysql

Implement specialization in ER diagram How to post query parameters with Axios? PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' is not supported How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Connection Java-MySql : Public Key Retrieval is not allowed How to grant all privileges to root user in MySQL 8.0 MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Examples related to codeigniter

DataTables: Cannot read property 'length' of undefined How to set time zone in codeigniter? php codeigniter count rows CodeIgniter: 404 Page Not Found on Live Server Only variable references should be returned by reference - Codeigniter How to pass a parameter like title, summary and image in a Facebook sharer URL How to JOIN three tables in Codeigniter how to get the base url in javascript How to get id from URL in codeigniter? How to disable PHP Error reporting in CodeIgniter?