Reputation: 441
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'
Upvotes: 1
Views: 80
Reputation: 441
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');
Upvotes: 0
Reputation: 1094
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();
Upvotes: 1