繁体   English   中英

CodeIgniter mySQL 2表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不存在。 当两个表都包含匹配users.user_id的数据时,我才会得到结果。

有什么建议么?

谢谢!

编辑为避免任何混淆,此查询为我提供了我需要的结果,但我想使用CodeIgniter数据库对象。

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为空,这也会产生结果。

你想把你的'left outer'放在join()函数中,而不是where()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM