[英]How to INNER JOIN 3 tables using CodeIgniter
Can someone Tell me how to join 3 table with php? 有人能告诉我如何用PHP加入3表吗? 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. 让我有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表。
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活动记录框架
I believe that using CodeIgniters active record framework that you would just use two join statements one after the other. 我相信使用CodeIgniters活动记录框架,您将一个接一个地使用两个连接语句。
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. 尝试一下,看看它是怎么回事。
I think in CodeIgniter the best to use ActiveRecord as wrote above. 我认为在CodeIgniter中最好使用ActiveRecord,如上所述。 One more thing: you can use method chaining in AR:
还有一件事:您可以在AR中使用方法链接:
$this->db->select('*')->from('table1')->join('table2','table1.id=table2.id')->...
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 我希望可以解决你的SQL
For executing pure SQL statements (I Don't Know About the FRAMEWORK- CodeIGNITER!!!) you can use SUB QUERY! 为了执行纯SQL语句(我不知道FRAMEWORK- CodeignITER !!!)你可以使用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; SELECT t1.id FROM例如点t1 INNER JOIN(自(示例2选择ID
t1
加入示例3 t2
上t1
。 id
= t2
。 id
为t2 ON t1.id = t2.id));
Hope you Get My Point! 希望你明白我的意思!
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');
}
$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. 您可以在哪里指定应查看哪个ID或在特定表中选择。 You can also select which join portion either left, right, outer, inner, left outer, and right outer on the third parameter of join method.
您还可以在join方法的第三个参数中选择左,右,外,内,左外和右外的连接部分。
$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()函数的第三个变量中使用任何类型的连接,如inner,left,right等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.