php - 我想在 codeigniter 中加入 4 个表

标签 php mysql sql codeigniter

直到知道我加入了 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(); 

连接表后我的输出是这样的 enter image description here

在上图中,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  

我想根据第 4 个表 (ts_users) 中的 ConsignorConsignee 获取全名

最佳答案

由于您并没有真正告诉我们 consignorconsignee 字段的来源,因此您需要填写空白(表示为 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/

相关文章:

mysql - 更新 Mysql 查询优化

java - 带返回列表 hibernate 的调用过程

php - 如何在 Laravel 5.5 中将数据从请求自动映射到模型

php - Laravel 5.5 - 从 API Resource 类中,如何获取底层模型类名称?

java 准备好的语句查询返回错误,但如果我打印它并直接在数据库上执行,它就可以工作

php - 如何在MySQL中自动更新表中依赖于其他列的列?

php - 使用 PHP 和 MySQL 更新记录中的 BLOB

mysql - SQL帮助——更新一个表中的字段以匹配另一个表中的字段基于匹配不同的字段

php - 如何仅删除页面的自动段落格式,而不是帖子(WordPress)

javascript - 如何将 codeigniter 的 CSRF token 传递给 Paypal Express Checkout?