直到知道我加入了 3 table
$this->db->select('*');
$this->db->from('dispatch_challan');
$this->db->join('challan_bilties', 'dispatch_challan.disp_id = challan_bilties.challan_id');
$this->db->join('bilty', 'challan_bilties.challan_bilties_id = bilty.id');
$this->db->where('dispatch_challan.disp_ch_no',$disp_ch_no);
$query = $this->db->get();
return $query->result_array();
在上图中,Consignor
和 Consignee
获取 id,但我想要名称,所以我想加入第 4 个表,即 ts_users
在这个表中这个Consignor
和Consignee
的全名
ts_users table
user_id user_fullname user_remark
1 abc consignee
2 xyz consignor
3 pqr consignee
4 lmn consignor
我想根据第 4 个表 (ts_users)
中的 Consignor
和 Consignee
获取全名
最佳答案
由于您并没有真正告诉我们 consignor
和 consignee
字段的来源,因此您需要填写空白(表示为 xxx
下面)
您只需要与 ts_users
表连接两次即可。添加这些:
$this->db->join('ts_users u1', 'xxx.consignor = u1.user_id');
$this->db->join('ts_users u2', 'xxx.consignee = u2.user_id');
由于您将使用同一个表加入两次,如果您坚持使用 select *
,您的结果集会有点困惑,所以我建议只获取您需要的字段。例如:
$this->db->select('u1.user_id as consignor, u2.user_id as consignee, dispatch_challan.*, challan_bilties.*, bilty.*');
试一试,如果有效请告诉我
关于php - 我想在 codeigniter 中加入 4 个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53651108/