Skip to content
Advertisement

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'

Advertisement

Answer

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');
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement