CodeIgniter - 如何在一个查询中组合 where() 和 where_not_in()?
CodeIgniter - How to combine where() and where_not_in() in one query?
在我的一个基于 CodeIgniter 的应用程序中,我需要结合使用 where() 和 where_not_in() 在一个查询中。我写的查询是:
$where = array(
'proj.project_code' => $project_code
);
$where_not_in = array();
if( $open_ticket == 1 ) {
$where_not_in['tic.status_id'] = $this->getStatusIdByStatusName('draft');
$where_not_in['tic.status_id'] = $this->getStatusIdByStatusName('voided');
$where_not_in['tic.status_id'] = $this->getStatusIdByStatusName('closed');
} else {
$where_not_in['tic.status_id'] = $this->getStatusIdByStatusName('new');
$where_not_in['tic.status_id'] = $this->getStatusIdByStatusName('waiting');
$where_not_in['tic.status_id'] = $this->getStatusIdByStatusName('on hold');
$where_not_in['tic.status_id'] = $this->getStatusIdByStatusName('in progress');
}
$this->db->select('tic.id id, prio.name priority, tic.title title, issue.name type, parent.ticket_code parent_code, par_proj.project_code par_proj_code, status.name status, usr.first_name first_name, usr.last_name last_name, count(exempt.id) exempt, tic.last_edit last_edit, tic.due_date due_date, tic.ticket_code ticket_code, proj.project_code project_code, par_proj.id par_proj_id');
$this->db->from('tickets tic');
$this->db->join('priorities prio', 'prio.id = tic.priority_id', 'left');
$this->db->join('issue_types issue', 'issue.id = tic.issue_type_id', 'left');
$this->db->join('statuses status', 'status.id = tic.status_id', 'left');
$this->db->join('projects proj', 'proj.id = tic.project_id', 'left');
$this->db->join('users usr', 'usr.id = tic.assignee_id', 'left');
$this->db->join('tickets parent', 'parent.id = tic.parent_id', 'left');
$this->db->join('projects par_proj', 'par_proj.id = parent.project_id', 'left');
$this->db->join('exemption_requests exempt', 'exempt.ticket_id = tic.id', 'left');
$this->db->where( $where );
$this->db->where_not_in( $where_not_in );
$this->db->group_by('tic.id, prio.name, tic.title, issue.name, parent.ticket_code, par_proj.project_code, status.name, usr.first_name, usr.last_name, tic.last_edit, tic.due_date');
$this->db->order_by('tic.id', 'ASC');
$this->db->limit( $limit, ($page_no - 1) * $limit );
这里:
- $open_ticket 从 views 发送并且是 0 或 1,
- getStatusIdByStatusName(status_name) returns 来自 的 primary_id状态 table
- $project_code,$page_no,$limit 发送自浏览量
所以当我测试结果时,它没有按预期过滤数据,这就是为什么我通过 CodeIgniter 打印生成的查询,这里是:
SELECT `tic`.`id` `id`, `prio`.`name` `priority`, `tic`.`title` `title`, `issue`.`name` `type`, `parent`.`ticket_code` `parent_code`, `par_proj`.`project_code` `par_proj_code`, `status`.`name` `status`, `usr`.`first_name` `first_name`, `usr`.`last_name` `last_name`, count(exempt.id) exempt, `tic`.`last_edit` `last_edit`, `tic`.`due_date` `due_date`, `tic`.`ticket_code` `ticket_code`, `proj`.`project_code` `project_code`, `par_proj`.`id` `par_proj_id`
FROM `tickets` `tic`
LEFT JOIN `priorities` `prio` ON `prio`.`id` = `tic`.`priority_id`
LEFT JOIN `issue_types` `issue` ON `issue`.`id` = `tic`.`issue_type_id`
LEFT JOIN `statuses` `status` ON `status`.`id` = `tic`.`status_id`
LEFT JOIN `projects` `proj` ON `proj`.`id` = `tic`.`project_id`
LEFT JOIN `users` `usr` ON `usr`.`id` = `tic`.`assignee_id`
LEFT JOIN `tickets` `parent` ON `parent`.`id` = `tic`.`parent_id`
LEFT JOIN `projects` `par_proj` ON `par_proj`.`id` = `parent`.`project_id`
LEFT JOIN `exemption_requests` `exempt` ON `exempt`.`ticket_id` = `tic`.`id`
WHERE `proj`.`project_code` = '510042520'
GROUP BY `tic`.`id`, `prio`.`name`, `tic`.`title`, `issue`.`name`, `parent`.`ticket_code`, `par_proj`.`project_code`, `status`.`name`, `usr`.`first_name`, `usr`.`last_name`, `tic`.`last_edit`, `tic`.`due_date`
ORDER BY `tic`.`id` ASC
LIMIT 20
如您所见,它不包括 where_not_in() 的条件。我不知道如何解决这个问题!
谁能告诉我如何将 where() 和 where_not_in() 结合在一个查询中。
- 谢谢
您的 where_not_in()
应该有两个参数 - 第一个是键 tic.status_id
,第二个是数组。
将您的 $where_not_in
作业更改为:
if( $open_ticket == 1 ) {
$where_not_in[] = $this->getStatusIdByStatusName('draft');
$where_not_in[] = $this->getStatusIdByStatusName('voided');
$where_not_in[] = $this->getStatusIdByStatusName('closed');
} else {
$where_not_in[] = $this->getStatusIdByStatusName('new');
$where_not_in[] = $this->getStatusIdByStatusName('waiting');
$where_not_in[] = $this->getStatusIdByStatusName('on hold');
$where_not_in[] = $this->getStatusIdByStatusName('in progress');
}
并且 where_not_in()
呼叫:
$this->db->where_not_in('tic.status_id', $where_not_in);
where_not_in()
不支持与 where()
相同的关联数组参数。
在我的一个基于 CodeIgniter 的应用程序中,我需要结合使用 where() 和 where_not_in() 在一个查询中。我写的查询是:
$where = array(
'proj.project_code' => $project_code
);
$where_not_in = array();
if( $open_ticket == 1 ) {
$where_not_in['tic.status_id'] = $this->getStatusIdByStatusName('draft');
$where_not_in['tic.status_id'] = $this->getStatusIdByStatusName('voided');
$where_not_in['tic.status_id'] = $this->getStatusIdByStatusName('closed');
} else {
$where_not_in['tic.status_id'] = $this->getStatusIdByStatusName('new');
$where_not_in['tic.status_id'] = $this->getStatusIdByStatusName('waiting');
$where_not_in['tic.status_id'] = $this->getStatusIdByStatusName('on hold');
$where_not_in['tic.status_id'] = $this->getStatusIdByStatusName('in progress');
}
$this->db->select('tic.id id, prio.name priority, tic.title title, issue.name type, parent.ticket_code parent_code, par_proj.project_code par_proj_code, status.name status, usr.first_name first_name, usr.last_name last_name, count(exempt.id) exempt, tic.last_edit last_edit, tic.due_date due_date, tic.ticket_code ticket_code, proj.project_code project_code, par_proj.id par_proj_id');
$this->db->from('tickets tic');
$this->db->join('priorities prio', 'prio.id = tic.priority_id', 'left');
$this->db->join('issue_types issue', 'issue.id = tic.issue_type_id', 'left');
$this->db->join('statuses status', 'status.id = tic.status_id', 'left');
$this->db->join('projects proj', 'proj.id = tic.project_id', 'left');
$this->db->join('users usr', 'usr.id = tic.assignee_id', 'left');
$this->db->join('tickets parent', 'parent.id = tic.parent_id', 'left');
$this->db->join('projects par_proj', 'par_proj.id = parent.project_id', 'left');
$this->db->join('exemption_requests exempt', 'exempt.ticket_id = tic.id', 'left');
$this->db->where( $where );
$this->db->where_not_in( $where_not_in );
$this->db->group_by('tic.id, prio.name, tic.title, issue.name, parent.ticket_code, par_proj.project_code, status.name, usr.first_name, usr.last_name, tic.last_edit, tic.due_date');
$this->db->order_by('tic.id', 'ASC');
$this->db->limit( $limit, ($page_no - 1) * $limit );
这里:
- $open_ticket 从 views 发送并且是 0 或 1,
- getStatusIdByStatusName(status_name) returns 来自 的 primary_id状态 table
- $project_code,$page_no,$limit 发送自浏览量
所以当我测试结果时,它没有按预期过滤数据,这就是为什么我通过 CodeIgniter 打印生成的查询,这里是:
SELECT `tic`.`id` `id`, `prio`.`name` `priority`, `tic`.`title` `title`, `issue`.`name` `type`, `parent`.`ticket_code` `parent_code`, `par_proj`.`project_code` `par_proj_code`, `status`.`name` `status`, `usr`.`first_name` `first_name`, `usr`.`last_name` `last_name`, count(exempt.id) exempt, `tic`.`last_edit` `last_edit`, `tic`.`due_date` `due_date`, `tic`.`ticket_code` `ticket_code`, `proj`.`project_code` `project_code`, `par_proj`.`id` `par_proj_id`
FROM `tickets` `tic`
LEFT JOIN `priorities` `prio` ON `prio`.`id` = `tic`.`priority_id`
LEFT JOIN `issue_types` `issue` ON `issue`.`id` = `tic`.`issue_type_id`
LEFT JOIN `statuses` `status` ON `status`.`id` = `tic`.`status_id`
LEFT JOIN `projects` `proj` ON `proj`.`id` = `tic`.`project_id`
LEFT JOIN `users` `usr` ON `usr`.`id` = `tic`.`assignee_id`
LEFT JOIN `tickets` `parent` ON `parent`.`id` = `tic`.`parent_id`
LEFT JOIN `projects` `par_proj` ON `par_proj`.`id` = `parent`.`project_id`
LEFT JOIN `exemption_requests` `exempt` ON `exempt`.`ticket_id` = `tic`.`id`
WHERE `proj`.`project_code` = '510042520'
GROUP BY `tic`.`id`, `prio`.`name`, `tic`.`title`, `issue`.`name`, `parent`.`ticket_code`, `par_proj`.`project_code`, `status`.`name`, `usr`.`first_name`, `usr`.`last_name`, `tic`.`last_edit`, `tic`.`due_date`
ORDER BY `tic`.`id` ASC
LIMIT 20
如您所见,它不包括 where_not_in() 的条件。我不知道如何解决这个问题!
谁能告诉我如何将 where() 和 where_not_in() 结合在一个查询中。
- 谢谢
您的 where_not_in()
应该有两个参数 - 第一个是键 tic.status_id
,第二个是数组。
将您的 $where_not_in
作业更改为:
if( $open_ticket == 1 ) {
$where_not_in[] = $this->getStatusIdByStatusName('draft');
$where_not_in[] = $this->getStatusIdByStatusName('voided');
$where_not_in[] = $this->getStatusIdByStatusName('closed');
} else {
$where_not_in[] = $this->getStatusIdByStatusName('new');
$where_not_in[] = $this->getStatusIdByStatusName('waiting');
$where_not_in[] = $this->getStatusIdByStatusName('on hold');
$where_not_in[] = $this->getStatusIdByStatusName('in progress');
}
并且 where_not_in()
呼叫:
$this->db->where_not_in('tic.status_id', $where_not_in);
where_not_in()
不支持与 where()
相同的关联数组参数。