我到目前为止能够在zend2 SELECT查询中应用AND和LIKE,但无法弄清楚如何应用OR运算符。这是我的问题:
$this->table = $data['table'];
$select = new Select();
$spec = function (Where $where) {
$where->like('title', 'border%');
};
$select->from($this->table)
->where(array('id'=> 8));
$select->where($spec);
$resultSet = $this->selectWith($select);
$resultSet->buffer();
return $resultSet;
返回此sql语句
SELECT `rs_morning_report`.*
FROM `rs_morning_report`
WHERE `id` = :where1 AND `title` LIKE :where2
现在我要添加“AND(扇区LIKE%a%OR sector LIKE%b%)”
答案 0 :(得分:4)
您可以使用Where对象链接它们。
$where = new Where();
$where->equalTo('fieldOne', 'XX')
->AND
->equalTo('field_two', 'XXX')
->OR
->equalTo('field_three', 'XXX')
;
$select->where($where);
如果需要,您也可以嵌套它们:
$where = new Where();
$where->equalTo('fieldOne', 'XX')
->NEST
->equalTo('field_two', 'XXX')
->OR
->equalTo('field_three', 'XXX')
->UNNEST
;
$select->where($where);
答案 1 :(得分:0)
要生成OR
语句,您可以将Zend\Db\Sql\Predicate\PredicateSet
与参数PredicateSet::COMBINED_BY_OR
一起使用。
示例:
use Zend\Db\Sql\Predicate;
$select->where(array(
// Other conditions...
new Predicate\PredicateSet(
array(
new Predicate\Like('sector', '%'.$sectorA.'%'),
new Predicate\Like('sector', '%'.$sectorB.'%'),
),
Predicate\PredicateSet::COMBINED_BY_OR
),
));