如何在Zend \ Db \ Sql中向数据库查询添加GREATER_THAN / LESS_THAN运算符?

时间:2013-12-13 17:00:30

标签: sql database zend-framework2

我需要在查询中添加特殊运算符。查询:

SELECT "contests".*
FROM "contests" WHERE "status" = '-1' 
AND "level1" = '1' AND "level2" = '1' AND "level3" = '1' AND "level10" = '1' 
AND DATE(`contests`.`start`) >= '2012-03-17' AND DATE(`contests`.`stop`) <= '2013-12-20'

在Zend Framework 2中:

$from = '2012-03-17';
$to = '2013-12-20';
  $sql = new Sql($this->adapter);
  $select = $sql->select('contests')
     ->where($where, $combination = new Predicate\Operator($from, Predicate\Operator::OPERATOR_GREATER_THAN_OR_EQUAL_TO, $to));

但这不起作用。

2 个答案:

答案 0 :(得分:4)

如果你写下这样的where子句怎么样: -

$select->where->equalTo('status', '-1'); 
$select->where->equalTo('level1', '1'); 

$select->where->greaterThanOrEqualTo('start', '2012-03-17');
$select->where->greaterThanOrEqualTo('stop', '2013-12-20');

答案 1 :(得分:1)

我正在使用表网关。使用起来更清楚。 你可以使用where子句。

public function getGelecek()
{
    $select = $this->tableGateway->getSql()->select();
    $select->where(array('active' => '1'));
    $select->where->greaterThan('tarih', '123');
    $resultset = $this->tableGateway->selectWith($select);
    return $resultset;
}