CodeIgniter Query Builder Class 无法像正常 SQL 查询那样获得多个 where 结果

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

问题:-

如果你想先执行第一个然后最后一个,如何在多个where之间添加()。因为您可以在下面提到的 sql 中添加括号。

Ci查询

$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'**

我要的是下面这样

SELECT COUNT(*) AS `numrows`
FROM `patient_details`
WHERE (`pt_id` = '79'
OR `sub_pt_id` IN('80'))
AND `status` = 'Complete'

您需要使用 group_start()group_end() 函数。它们的工作方式类似于 开括号闭括号https://www.codeigniter.com/userguide3/database/query_builder.html#query-grouping

您的代码将如下所示:

$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();

解决方案 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');