Zend SQL - 条件where子句

时间:2013-12-16 13:34:56

标签: php mysql sql zend-framework

我有以下Zend SQL查询:

$select = $this->select()
->where($this->_quote('VerDate <= ?', $todate))
->where($this->_quote('VerFieldId = ?', $fieldid));
if (count($records) > 0) {
    $this->select()->where($this->_quote('VerRecordId IN (?)', $records));
}
$this->select()->group('VerRecordId');

如果已将$ record数组传递给函数,我只想在VerRecordId上添加where条件。

当我在VerFieldId上的where子句之后停止查询时。

任何人都可以帮我修改查询吗?

更新:

我已使用以下代码解决了该问题。但是,如果有人知道这是否可以更好地构建,请告诉我。

$where = array();
$where['VerDate <= ?'] = $todate;
$where['VerFieldId = ?'] = $fieldid;
if (count($records) > 0) {
    $where['VerRecordId IN (?)'] = $records;
}
$select = $this->select();
$this->_where($select,$where);
$select->group('VerRecordId');

这允许我有条件地添加VerRecordId条件。

1 个答案:

答案 0 :(得分:1)

  

ZF2

 $sql = new Sql($this->adapter);
          $select = new Select($this->table);
          $where = new  \Zend\Db\Sql\Where();
          $where->equalTo('field1',(string) $value1);
          $where->greaterThan('field2','value2') ;
          $where->equalTo('field3','value3');
          $select->where($where);
          $statement = $sql->prepareStatementForSqlObject($select);
          $result = $statement->execute();
  

ZF1

$select = $this->select();      
$select->setIntegrityCheck(false) // if you have joins
            ->from(array('tablename' => 'tablename',))
            $select->where('field = ?', $value);
$select->where(new Zend_Db_Expr("field <> 'value'"));   
        $result = $this->fetchAll($select); 

然后你可以循环$ result

我希望它可以帮助你

http://framework.zend.com/manual/2.2/en/modules/zend.db.sql.html