everyone.
I'm using CodeIgniter, and I'm not getting results for this query:
$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.
$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 .
Any suggestions?
Thanks!
EDIT To avoid any confusion, this query gives me the results I need, but I want to use the CodeIgniter db objects.
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.
Inside this article we will see the concept of Left Join in CodeIgniter 4. Joins in CodeIgniter 4 is the connection between one and more tables to get data. In MySQL we have Inner join, Left join, Right join, Outer join. We will see the concept of Left Join in this CodeIgniter 4 article.
CodeIgniter Left Join Query: The LEFT JOIN is sometimes called as LEFT OUTER JOIN and is performed on two database tables - the first table (on the left side) is referred as LEFT TABLE and and the second (on the right side) one as RIGHT TABLE.
Hi this will work for joining two tables in codeIgnator. You can change to the query as you like do trail and error method to get your appropriate results. Show activity on this post.
Hit the URL http://localhost/codeIgniter-full-outer-join/index.php, you will see below output on the browser. You will see more blogs than what you see here in the image.
You want to put your 'left outer' in the join() function, not the where()
$this->db->join('show_guides', 'show_guides.user_id = users.user_id', 'left outer');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With