CodeIgniter 2 我需要这个,但我需要的值仍然被转义,所以csotelo 的答案对我不利,我不想像Shawn C 的答案那样重写整个查询,所以我最终这样做了:
$this->db->where('LastName', $lastName);
$this->db->where('Age', $age, false);
$this->db->where('1 AND ( 0', null, false); // This one starts the group
$this->db->or_where('FirstName', $firstName1);
$this->db->or_where('FirstName', $firstName2);
$this->db->or_where('Gender', $gender);
$this->db->or_where('Country', $country);
$this->db->or_where('0 )', null, false); // This one ends the group
$query = $this->db->get('Persons');
这会生成以下查询:
SELECT *
FROM (`Persons`)
WHERE `LastName` = 'Svendson'
AND Age = 12
AND 1 AND ( 0 -- This one starts the group
OR `FirstName` = 'Tove'
OR `FirstName` = 'Ola'
OR `Gender` = 'M'
OR `Country` = 'India'
OR 0 ) -- This one ends the group
这将与此相同:
SELECT * FROM (`Persons`) WHERE
`LastName` = 'Svendson' AND Age = 12 AND 1 AND
(0 OR `FirstName` = 'Tove' OR `FirstName` = 'Ola' OR `Gender` = 'M' OR `Country` = 'India' OR 0)