php - Zend SQL - 条件 where 子句

标签 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');

如果 $records 数组已传递给函数,我只想在 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 条件。

最佳答案

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

关于php - Zend SQL - 条件 where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20612276/

相关文章:

android - 关于在ListView上显示的高效查询

sql - 如何在SQL语句中使用count(*)和除法运算

mysql - logstash mysql general_logs CSV 格式

从两个表中选择 PHP

php - MySQL 查询在 PHP 中使用时返回空结果,但在 MySQL 客户端中完美运行

php - 将 Symfony 3 应用程序连接到( Asterisk 服务器)中的外部 SQL 数据库

当我有保持打开的 sleep 连接时 MySQL CPU 增加

mysql - 查询内查询

SQL 条件相交

java - 将 Json 从 Java 应用程序传递到 PHP (Laravel)