首页 > 解决方案 > 我想在 codeigniter 中加入 4 个表

问题描述

直到知道我加入了 3 张桌子

$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(); 

加入表后我的输出是这样的 在此处输入图像描述

在上图中ConsignorConsignee 获取 id 但我想要名称,所以我想加入第 4 个表,即ts_users

在这个表中这个的全名ConsignorConsignee

                           ts_users table

               user_id  user_fullname  user_remark
                 1            abc         consignee
                 2            xyz         consignor
                 3            pqr         consignee
                 4            lmn         consignor  

我想根据第四张表Consignor获得全名Consignee(ts_users)

标签: phpmysqlsqlcodeigniter

解决方案


由于您并没有真正告诉我们consignorandconsignee字段的来源,因此您需要填写空白(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.*');

试一试,让我知道它是否有效


推荐阅读