我想在括号中多个喜欢条件。
这是我的CI Active Record
代码: -
$this->db->select("ev.`created_date`,u.first_name,u.last_name,ev.`expert_id` , ev.`posted_by` ,
ev.`segment_id` , ev.`expert_title` , ev.`expert_discription` , ev.`status` , s.segment", FALSE);
$this->db->from($this->db->dbprefix . 'expert_view as ev');
$this->db->join($this->db->dbprefix . 'segment as s', 's.segment_id=ev.segment_id');
$this->db->join($this->db->dbprefix . 'users as u', 'u.id=ev.posted_by');
if ($this->session->userdata('group_id') != 1) {
$this->db->where('ev.posted_by', $this->session->userdata('userid'));
}
if ($sSearch) {
$this->db->like('u.first_name', $sSearch);
$this->db->or_like('u.last_name', $sSearch);
$this->db->or_like('ev.created_date', $sSearch);
$this->db->or_like('ev.posted_by', $sSearch);
$this->db->or_like('ev.`expert_title`', $sSearch);
$this->db->or_like('s.`segment`', $sSearch);
}
echo $this->db->last_query();
输出来自上面的代码:
SELECT ev.`created_date`, u.first_name, u.last_name, ev.`expert_id`, ev.`posted_by`, ev.`segment_id`
, ev.`expert_title`, ev.`expert_discription`, ev.`status`, s.segment
FROM (`adv_expert_view` as ev)
JOIN `adv_segment` as s ON `s`.`segment_id`=`ev`.`segment_id`
JOIN `adv_users` as u ON `u`.`id`=`ev`.`posted_by`
WHERE `ev`.`posted_by` = '14'
AND `u`.`first_name` LIKE '%a%'
OR `u`.`last_name` LIKE '%a%'
OR `ev`.`created_date` LIKE '%a%'
OR `ev`.`expert_title` LIKE '%a%'
OR `s`.`segment` LIKE '%a%'
预期查询
SELECT ev.`created_date`, u.first_name, u.last_name, ev.`expert_id`, ev.`posted_by`, ev.`segment_id`
, ev.`expert_title`, ev.`expert_discription`, ev.`status`, s.segment
FROM (`adv_expert_view` as ev)
JOIN `adv_segment` as s ON `s`.`segment_id`=`ev`.`segment_id`
JOIN `adv_users` as u ON `u`.`id`=`ev`.`posted_by`
WHERE `ev`.`posted_by` = '14'
AND (`u`.`first_name` LIKE '%a%'
OR `u`.`last_name` LIKE '%a%'
OR `ev`.`created_date` LIKE '%a%'
OR `ev`.`expert_title` LIKE '%a%'
OR `s`.`segment` LIKE '%a%')
答案 0 :(得分:2)
如果您使用CI 3您可以使用->group_start()
和->group_end()
添加括号。有关详细信息,请阅读Query grouping
$this->db->group_start()
$this->db->like('u.first_name', $sSearch);
$this->db->or_like('u.last_name', $sSearch);
$this->db->or_like('ev.created_date', $sSearch);
$this->db->or_like('ev.posted_by', $sSearch);
$this->db->or_like('ev.`expert_title`', $sSearch);
$this->db->or_like('s.`segment`', $sSearch);
$this->db->group_end()
FOR CI 2您可以创建条件
$this->db->where("(`u`.`first_name` LIKE '%a%' OR `u`.`last_name` LIKE '%a%' OR `ev`.`created_date` LIKE '%a%' OR `ev`.`expert_title` LIKE '%a%' OR `s`.`segment` LIKE '%a%')");
答案 1 :(得分:1)
这样做: -
$query = "SELECT ev.`created_date`, u.first_name, u.last_name, ev.`expert_id`, ev.`posted_by`, ev.`segment_id`
, ev.`expert_title`, ev.`expert_discription`, ev.`status`, s.segment
FROM (`adv_expert_view` as ev)
JOIN `adv_segment` as s ON `s`.`segment_id`=`ev`.`segment_id`
JOIN `adv_users` as u ON `u`.`id`=`ev`.`posted_by`";
$query.= " WHERE";
if ($this->session->userdata('group_id') != 1) {
$query.= " `ev`.`posted_by` =" . $this->session->userdata('userid') . " AND";
}
if ($sSearch) {
$query.= " (`u`.`first_name` LIKE '%$sSearch%'
OR `u`.`last_name` LIKE '%$sSearch%'
OR `ev`.`created_date` LIKE '%$sSearch%'
OR `ev`.`expert_title` LIKE '%$sSearch%'
OR `s`.`segment` LIKE '%$sSearch%')";
}
答案 2 :(得分:0)
试试这个
$this->db->where("(`u`.`first_name`LIKE '%a%'OR `u`.`last_name` LIKE '%a%' OR `ev`.`created_date` LIKE '%a%' OR `ev`.`expert_title` LIKE '%a%' OR `s`.`segment` LIKE '%a%')");