Codeigniter Active Record / MySQL Join Query - How to Return Results if one of the Table Rows is Not Present

时间:2022-09-05 15:46:40

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. Please could someone advise how to achieve this?

问题是,表3中可能没有行,如果没有,我仍然希望返回带有剩余查询数据的结果。有人可以建议如何实现这一目标吗?

2 个解决方案

#1


1  

you should do a left join on table3

你应该在table3上做一个左连接

#2


0  

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');

#1


1  

you should do a left join on table3

你应该在table3上做一个左连接

#2


0  

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');