|
|
|
สอบถามเรื่องการ Join 3 ตาราง CI ครับ พอดีลองทำแล้ว งงๆ ไม่ทราบว่าถูกไหมครับ |
|
|
|
|
|
|
|
Code (PHP)
$query = $this->db->order_by('a.table_1', 'DESC')
->join('table_2 b', 'b.ref_id_table_1 = a.id_table_1')
->join('table_3 c', 'c.ref_id_table_1 = a.id_table_1')
->get_where('table_1 a', ['ref_id_user'=>$id_user])->result();
if($this->db->error()['code']!=0){ // only on production
die(print_r($this->db->error(),true));
}
foreach ($query as $row) {
echo '<pre>',print_r( $row, true),'</pre>';
}
|
ประวัติการแก้ไข 2018-10-23 16:40:20
|
|
|
|
Date :
2018-10-23 16:39:39 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้แล้วครับผมขอบคุณครับ
public function show_data($id_user) {
$this->db->select('*');
$this->db->from('table_1');
$this->db->join('table_2', 'table_2.ref_id_table_1 = table_1.id_table_1');
$this->db->join('table_3', 'table_3.ref_id_table_1 = table_1.id_table_1');
$this->db->where('table_1.ref_id_user', $id_user);
$this->db->where('table_2.ref_id_user', $id_user);
$this->db->where('table_3.ref_id_user', $id_user);
$this->db->order_by('id_table_1','id_table_2','id_table_3', 'DESC');
$query = $this->db->get();
return $query->result_array();
foreach ($query->result() as $row) {
$row->detail_1;
$row->detail_2;
$row->detail_3;
$row->detail_4;
$row->detail_5;
$row->detail_6;
$row->detail_7;
$row->detail_8;
$row->detail_9;
$row->detail_10;
$row->detail_11;
$row->detail_12;
$row->detail_13;
}
}
|
ประวัติการแก้ไข 2018-10-23 22:27:29
|
|
|
|
Date :
2018-10-23 22:25:59 |
By :
teedesign |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จะไปเขียนทำไมให้ยาว รูปแบบเขาให้เขียนสั้นๆ ได้
Code (PHP)
$this->db->select('*');
$this->db->from('table_1');
$this->db->join('table_2', 'table_2.ref_id_table_1 = table_1.id_table_1');
$this->db->join('table_3', 'table_3.ref_id_table_1 = table_1.id_table_1');
$this->db->where('table_1.ref_id_user', $id_user);
$this->db->where('table_2.ref_id_user', $id_user);
$this->db->where('table_3.ref_id_user', $id_user);
$this->db->order_by('id_table_1','id_table_2','id_table_3', 'DESC');
$query = $this->db->get();
// แก้เป็น
$query = $this->db
->join('table_2 t2', 't2.ref_id_table_1 = t1.id_table_1 and t2.ref_id_user = t1.ref_id_user')
->join('table_3 t3', 't3.ref_id_table_1 = t1.id_table_1 and t3.ref_id_user = t1.ref_id_user')
->get_where('table_1 t1', [t1.ref_id_user' => $id_user])->result();
|
|
|
|
|
Date :
2018-10-24 09:08:27 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|