php CodeIgniter mySQL 2 表左外连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8083332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter mySQL 2 table LEFT OUTER JOIN
提问by Charlie
everyone.
每个人。
I'm using CodeIgniter, and I'm not getting results for this query:
我正在使用 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;
}
The 'users' table will always have results, but sometimes the user won't have a row in the 'show_guides' table. When the 'show_guides' table doesn't have results, the query doesn't return results from the 'users' table.
'users' 表总是有结果,但有时用户在 'show_guides' 表中没有一行。当“show_guides”表没有结果时,查询不会从“users”表返回结果。
$row doesn't exist when 'show_guides' produces no results. I only get results when both tables have data with the matching users.user_id .
当“show_guides”没有结果时,$row 不存在。只有当两个表都有匹配 users.user_id 的数据时,我才会得到结果。
Any suggestions?
有什么建议?
Thanks!
谢谢!
EDITTo avoid any confusion, this query gives me the results I need, but I want to use the CodeIgniter db objects.
编辑为避免任何混淆,此查询为我提供了所需的结果,但我想使用 CodeIgniter db 对象。
SELECT u.*,s.*
FROM users u
LEFT OUTER JOIN show_guides s ON u.user_id = s.user_id
WHERE u.user_id = 155;
This gives results even if show_guides is empty.
即使 show_guides 为空,这也会给出结果。
回答by dispake
You want to put your 'left outer' in the join() function, not the where()
你想把你的“左外”放在 join() 函数中,而不是 where()
$this->db->join('show_guides', 'show_guides.user_id = users.user_id', 'left outer');