[英]How to INNER JOIN 3 tables using CodeIgniter
有人能告诉我如何用PHP加入3表吗? 例
SELECT FROM table1, table2,table on INNERJOIN -------------------
让我有3个表。(问题表,答案表和类别表)这是我的网页的示例。
Time remaining 30 minutes(I will get "30 minutes" form Category table)
1. Question (from question table)
2. answer (from answer table)
我不知道如何加入3表。
它应该是那样的,
$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();
根据CodeIgniters活动记录框架
我相信使用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();
尝试一下,看看它是怎么回事。
我认为在CodeIgniter中最好使用ActiveRecord,如上所述。 还有一件事:您可以在AR中使用方法链接:
$this->db->select('*')->from('table1')->join('table2','table1.id=table2.id')->...
我创建了一个函数来获取一个包含字段值和连接的数组。 这在模型中:
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();
}
这在控制器中:
$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);
结果:
echo '<pre>';
print_r($product_category);
die;
你可以像这样修改你的编码
$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();
我希望可以解决你的SQL
为了执行纯SQL语句(我不知道FRAMEWORK- CodeignITER !!!)你可以使用SUB QUERY! 语法如下
SELECT t1.id FROM例如点t1 INNER JOIN(自(示例2选择ID t1
加入示例3 t2
上t1
。 id
= t2
。 id
为t2 ON t1.id = t2.id));
希望你明白我的意思!
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();
您可以在哪里指定应查看哪个ID或在特定表中选择。 您还可以在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();
这将给你table1,table2,table3的结果,你可以在$ this-> db-> join()函数的第三个变量中使用任何类型的连接,如inner,left,right等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.