CodeIgniter mySQL 2 table LEFT OUTER JOIN


CodeIgniter mySQL 2 table LEFT OUTER JOIN

每个人。

我正在使用CodeIgniter,但我没有得到这个查询的结果:

    $this->load->database();
    $this->db->select('*');
    $this->db->from('users');
    $this->db->join('show_guides', 'show_guides.user_id = users.user_id');
    $this->db->where('users.user_id', $user_id['user_id'], 'left outer');
    $query = $this->db->get();
    foreach ($query->result_array() as $row) {
        $results = $row;
    }

"users"表总是有结果,但有时用户在"show_guides"表中没有一行。当"show_guides"表没有结果时,查询不会从"users"表返回结果。

当"show_guides"未产生结果时,$row不存在。只有当两个表都有匹配用户的数据时,我才会得到结果。user_id.

有什么建议吗?

谢谢!

编辑为了避免任何混淆,这个查询为我提供了所需的结果,但我希望使用CodeIgniterdb对象。

SELECT u.*,s.* 
FROM users u
LEFT OUTER JOIN show_guides s ON u.user_id = s.user_id
WHERE u.user_id = 155;

即使show_guides为空,也会显示结果。

您希望将"左外部"放在join((函数中,而不是where((

$this->db->join('show_guides', 'show_guides.user_id = users.user_id', 'left outer');