php 如何使用 CodeIgniter 对 3 个表进行 INNER JOIN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7440371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:40:38  来源:igfitidea点击:

How to INNER JOIN 3 tables using CodeIgniter

phpmysqlcodeigniter

提问by Wai Yan

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

有人可以告诉我如何用php加入3个表吗?例子

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.

让我有 3 个表。(问题表、答案表和类别表)这是我网页的示例。

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.

我不知道如何加入 3 表。

回答by Ket.

it should be like that,

应该是这样的

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

as per CodeIgniters active record framework

根据 CodeIgniters 活动记录框架

回答by Klee

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

我相信使用 CodeIgniters 活动记录框架,您只需一个接一个地使用两个连接语句。
例如:

$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.

试一试,看看效果如何。

回答by uzsolt

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

我认为在 CodeIgniter 中最好使用上面写的 ActiveRecord。还有一件事:你可以在 AR 中使用方法链:

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

回答by Fabio Souza

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;

回答by Tirth Bodawala

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

对于执行纯 SQL 语句(我不知道框架 - CodeIGNITER !!!)你可以使用 SUB QUERY!语法如下

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

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

Hope you Get My Point!

希望你得到我的观点!

回答by lothux1987

$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.

您可以在其中指定应查看或在特定表中选择的 ID。您还可以在 join 方法的第三个参数上选择左、右、外、内、左外和右外的连接部分。

回答by Endang Taryana

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

我希望可以解决您的 SQL

回答by Manish

$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.

这将为您提供 table1、table2、table3 的结果,您可以在 $this->db->join() 函数的第三个变量中使用任何类型的连接,例如内、左、右等。

回答by ajo

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');
}