[英]Codeigniter Active Record / MySQL Join Query - How to Return Results if one of the Table Rows is Not Present
I have the following query: 我有以下查询:
$this->db
->select('SQL_CALC_FOUND_ROWS null as rows
,table1.*
,table2.*
,table3.*', FALSE)
->from('table1')
->where('table1.column1', $user_id)
->join('table2', 'table2.column2 = table1.column2')
->join('table3', 'table3.column2 = table1.column2')
->group_by('table1.column2')
->order_by('table1.column2', 'DESC');
$query = $this->db->get();
The problem is, there may not be a row in table 3 and if there is not, I would still like to return a result with the remainder of the query data. 问题是,表3中可能没有一行,如果没有,我仍然想返回带有查询数据其余部分的结果。 Please could someone advise how to achieve this? 请有人建议如何实现这一目标?
您应该在table3上进行左连接
Use left join and also use group_by to get exact records: 使用左联接,还使用group_by获取确切的记录:
$this->db->join('login_ranknames AS t4', 't4.id = t1.rank', 'left');
$this->db->group_by('t4.id');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.