Join query with multiple ON condition not working in codeigniter? - php

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

Related

CodeIgniter Query Builder Class failed to achieve multiple where result as normal SQL query

Problem:-
How to add () between multiple where if you want to execute first 2 where first then last one. as you can add bracket in sql mention below.
Ci Query
$this->db->select("*");
$this->db->from('patient_details');
$this->db->where('pt_id', $login_user_id);
$this->db->or_where_in('sub_pt_id', $implode_child_array);
$this->db->where('status', 'Complete');
$query = $this->db->count_all_results();
Sql
**SELECT COUNT(*)
AS `numrows`
FROM `patient_details`
WHERE `pt_id` = '79'
OR `sub_pt_id` IN('80')
AND `status` = 'Complete'**
What i want is like below
SELECT COUNT(*) AS `numrows`
FROM `patient_details`
WHERE (`pt_id` = '79'
OR `sub_pt_id` IN('80'))
AND `status` = 'Complete'
You need to use group_start() and group_end() functions. They work like open bracket and close bracket : https://www.codeigniter.com/userguide3/database/query_builder.html#query-grouping
Your code will look like this :
$this->db->select("*");
$this->db->from('patient_details');
$this->db->group_start();
$this->db->where('pt_id', $login_user_id);
$this->db->or_where_in('sub_pt_id', $implode_child_array);
$this->db->group_end();
$this->db->where('status', 'Complete');
$query = $this->db->count_all_results();
Solution
Query grouping
$this->db->group_start()
$this->db->where('pt_id', $login_user_id);
$this->db->or_where_in('sub_pt_id', $implode_child_array);
$this->db->group_end()
$this->db->where('status', 'Complete');

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

How to INNER JOIN 3 tables using CodeIgniter

Can someone Tell me how to join 3 table with php?
Example
SELECT FROM table1, table2,table on INNERJOIN -------------------
let I have 3 table.(question table ,answer table and category table)
Here is example form my webpage.
Time remaining 30 minutes(I will get "30 minutes" form Category table)
1. Question (from question table)
2. answer (from answer table)
I don't know how to join 3 table.
it should be like that,
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id');
$this->db->join('table3', 'table1.id = table3.id');
$query = $this->db->get();
as per CodeIgniters active record framework
I believe that using CodeIgniters active record framework that you would just use two join statements one after the other.
eg:
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table1', 'table1.id = table2.id');
$this->db->join('table1', 'table1.id = table3.id');
$query = $this->db->get();
Give that a try and see how it goes.
I created a function to get an array with the values ​​for the fields and to join. This goes in the model:
public function GetDataWhereExtenseJoin($table,$fields,$data) {
//pega os campos passados para o select
foreach($fields as $coll => $value){
$this->db->select($value);
}
//pega a tabela
$this->db->from($table);
//pega os campos do join
foreach($data as $coll => $value){
$this->db->join($coll, $value);
}
//obtem os valores
$query = $this->db->get();
//retorna o resultado
return $query->result();
}
This goes in the controller:
$data_field = array(
'NameProduct' => 'product.IdProduct',
'IdProduct' => 'product.NameProduct',
'NameCategory' => 'category.NameCategory',
'IdCategory' => 'category.IdCategory'
);
$data_join = array
( 'product' => 'product_category.IdProduct = product.IdProduct',
'category' => 'product_category.IdCategory = category.IdCategory',
'product' => 'product_category.IdProduct = product.IdProduct'
);
$product_category = $this->mmain->GetDataWhereExtenseJoin('product_category', $data_field, $data_join);
result:
echo '<pre>';
print_r($product_category);
die;
I think in CodeIgniter the best to use ActiveRecord as wrote above.
One more thing: you can use method chaining in AR:
$this->db->select('*')->from('table1')->join('table2','table1.id=table2.id')->...
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id','JOIN Type');
$this->db->join('table3', 'table1.id = table3.id');
$query = $this->db->get();
this will give you result from table1,table2,table3 and you can use any type of join in the third variable of $this->db->join() function such as inner,left, right etc.
For executing pure SQL statements (I Don't Know About the FRAMEWORK- CodeIGNITER!!!)
you can use SUB QUERY!
The Syntax Would be as follows
SELECT t1.id
FROM example t1 INNER JOIN
(select id from (example2 t1 join example3 t2 on t1.id = t2.id)) as t2 ON t1.id = t2.id;
Hope you Get My Point!
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id', 'inner');
$this->db->join('table3', 'table1.id = table3.id', 'inner');
$this->db->where("table1", $id );
$query = $this->db->get();
Where you can specify which id should be viewed or select in specific table. You can also select which join portion either left, right, outer, inner, left outer, and right outer on the third parameter of join method.
you can modiv your coding like this
$this->db->select('a.nik,b.nama,a.inv,c.cekin,c.cekout,a.tunai,a.nontunai,a.id');
$this->db->select('DATEDIFF (c.cekout, c.cekin) as lama');
$this->db->select('(DATEDIFF (c.cekout, c.cekin)*c.total) as tagihan');
$this->db->from('bayar as a');
$this->db->join('pelanggan as b', 'a.nik = b.nik');
$this->db->join('pesankamar_h as c', 'a.inv = c.id');
$this->db->where('a.user_id',$id);
$query = $this->db->get();
return $query->result();
i hope can be resolve your SQL
function fetch_comments($ticket_id){
$this->db->select('tbl_tickets_replies.comments,
tbl_users.username,tbl_roles.role_name');
$this->db->where('tbl_tickets_replies.ticket_id',$ticket_id);
$this->db->join('tbl_users','tbl_users.id = tbl_tickets_replies.user_id');
$this->db->join('tbl_roles','tbl_roles.role_id=tbl_tickets_replies.role_id');
return $this->db->get('tbl_tickets_replies');
}

Categories