Outer join not working in my app - php

Outer join in codeigniter not working
$this->db->select('*');
$this->db->from('users');
$this->db->join('userdetails','users.user_id=userdetails.user_id','outer');
$query = $this->db->get();
if($query->result())
return $query->result();
else
return false;
Please help

When you write an outer join in MySQL, the keyword LEFT or RIGHT is required, while OUTER is optional. So use:
$this->db->join('userdetails','users.user_id=userdetails.user_id','left');

Related

how to write simple query into codeigniter query using join right

how to write simple query into codeigniter query using join righ?????
$query = $this->db->query("Select staff_permissions_list.perm_type,staff_permissions_list.permission_key,staff_permissions_list.permission_label,
staff_permissions_list.id, staff_role_permissions.permission_id as p_id,staff_role_permissions.role_id
FROM staff_role_permissions
RIGHT JOIN staff_permissions_list ON staff_role_permissions.permission_id=staff_permissions_list.id
AND staff_role_permissions.role_id=$id WHERE staff_permissions_list.perm_type=0
ORDER BY staff_permissions_list.id ASC
");
if ($query->num_rows() > 0) {
return $query->result_array();
}
$this->db->select('book_id, book_name, author_name, category_name');
$this->db->from('books');
$this->db->join('category', 'category.category_id = books.category_id', 'right');
$query = $this->db->get();
you can get data using this method of right join
How about that ?
$query = $this->db
->select("Select staff_permissions_list.perm_type,staff_permissions_list.permission_key,staff_permissions_list.permission_label,staff_permissions_list.id, staff_role_permissions.permission_id as p_id,staff_role_permissions.role_id")
->from("staff_role_permissions AS srp")
->join("staff_permissions_list AS spl","srp.permission_id = spl.id","right")
->where("spl.perm_type","0")
->where("srp.role_id",$id)
->order_by("spl.id","ASC")
->get();
i put the role_id to the where section - maybe you need to put it back (not sure what you want to achieve here)

How to make Join Query model in CodeIgniter

How to write join query in codeigniter... I want only model like this Select query-
public function getData($col, $table, $where = array())
{
$this->db->select($col);
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;
}
Plz help
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
http://ellislab.com/codeigniter/user-guide/database/active_record.html#select
Please go through the user guide before posting it on stackoverflow
join query is there..
try this
$this->db->join('second_table', 'second_table.id = first_table.id');
try like this
$this->db->select('*');
$this->db->from('first_table');
$this->db->join('second_table', 'second_table.col_name = first_table.col_name');
$query=$this->db->get();
if($query->num_rows()>0){
return $query->result_array();
}
for joining table you may use join() methods.
$this->db->from(table1)
$this->db->join('table2','table1.id=table2.table1_id','join options');
on condition means in which condition you want to join tables .
Join options is optional.
Join Options are: left, right, outer, inner, left outer, and right outer

codeigniter where() postgresql

What is the format in codeigniter
SELECT seq,
code,
discipline,
bacc
FROM "psced-disc" p
JOIN table2 t
ON p.code=t.psced_id
WHERE t.tableID=table2;
I already tried this and its working but without the WHERE clause
$this->db->select('seq, code, discipline,bacc');
$this->db->from('psced-disc p');
$this->db->join('table2 t', 'p.code=t.psced_id');
$query = $this->db->get();
return $query->result();
I want to add the WHERE t.tableID=table2;
Can anyone help me with this. I'm getting stuck at it.
try this code:
$this->db->select('seq, code, discipline,bacc');
$this->db->from('psced-disc p');
$this->db->join('table2 t', 'p.code=t.psced_id');
$this->db->where(array("t.tableID"=>"table2")); // or $this->db->where("t.tableID = 'table2'");
$query = $this->db->get();
return $query->result();

Join query with multiple ON condition not working in codeigniter?

I need a query like this
SELECT * FROM (`users`)
LEFT JOIN `users_phone_numbers`
ON `users`.`id`= `users_phone_numbers`.`user_id`
LEFT JOIN `phone_number`
ON (`phone_number`.`id`= `users_phone_numbers`.`phone_num_id` AND users_phone_numbers.is_active = 1)
WHERE `users`.`id` = 56
i have code like this in codeigniter
$this->db->select('*');
$this->db->from('users');
$this->db->join('users_phone_numbers',
'users.id= users_phone_numbers.user_id',
'left');
$this->db->join('phone_number',
'(phone_number.id= users_phone_numbers.phone_num_id AND users_phone_numbers.is_active = 1)',
'left');
$this->db->where('users.id = '. $id);
$result = $q->result_array();
But i got this error
If you check Codeigniter's handling of the condition function, you will see the following:
// Strip apart the condition and protect the identifiers
if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
Codeigniter is stripping the opening bracket from the query. Can you not move the LEFT JOIN additional clause within the WHERE clause to get around this?
EDIT:
$this->db->select('*');
$this->db->from('users');
$this->db->join('users_phone_numbers',
'users.id= users_phone_numbers.user_id',
'left');
$this->db->join('phone_number',
'phone_number.id= users_phone_numbers.phone_num_id)',
'left');
$this->db->where(array('users.id => '. $id, 'users_phone_numbers.is_active' => 1));
$result = $q->result_array();
You need to remove the second condition of the join.
$this->db->join('phone_number',
'phone_number.id= users_phone_numbers.phone_num_id AND users_phone_numbers.is_active = 1',
'left');
remove brackets like above. it works for me when there is no bracket

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.
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');

Categories