php - 我想在 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();
在上图中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
我想根据第四张表Consignor
获得全名Consignee
(ts_users)
解决方案
由于您并没有真正告诉我们consignor
andconsignee
字段的来源,因此您需要填写空白(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 - MySql 5.7:一般错误:1366 Incorrect integer value: '' for TinyInt(1) field
- android - 如何在接收焦点时消失 TextInputEditText 中的提示
- .net - 在 dotnet 命令行中出现 SSL 错误
- r - 当先知模型循环运行时,R 崩溃
- xamarin.forms - 图像不会在 xamarin 中显示
- mysql - 如何在 mysql 查询中总计“AS”列?
- r - Continuous x 审美——你忘了 aes(group=...) 是什么意思吗?
- php - 在另一个文件中执行后如何使用php变量的值
- sql-server-2008 - SQL 查询 - 使用 openquery 无法正常工作
- python - 一次一批