Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter mySQL 2 table LEFT OUTER JOIN

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.

like image 384
Charlie Avatar asked Nov 10 '11 16:11

Charlie


People also ask

What is left join in Codeigniter 4?

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.

What is a left join query?

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.

Is it possible to join two tables in codeignator?

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.

How to view more blogs using CodeIgniter FULL OUTER JOIN?

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.


1 Answers

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');
like image 164
dispake Avatar answered Sep 30 '22 17:09

dispake