gpt4 book ai didi

php - Zend SQL - 条件 where 子句

转载 作者:行者123 更新时间:2023-11-29 03:02:44 25 4
gpt4 key购买 nike

我有以下 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com