php - ZF2 select 中的别名 - 带有计算字段

标签 php mysql zend-framework2 php-5.5

我想使用 ZF2 创建以下查询:

SELECT (a + b) AS c FROM ta WHERE c > 1;

有 table ta包含两个整数字段 ab .

到目前为止我已经尝试过使用以下代码:

$columns = array('c'=>'(a + b)');
$where = 'c > 1';
$tableGateway = $this->getTableGateway('ta');
$sql = $tableGateway->getSql();
$select = $sql->select()->columns($columns);
$select->where($where);    
$itemData = $tableGateway->selectWith($select);

不幸的是,返回的查询是:

SELECT `ta`.`a + b` AS `c` FROM `ta` WHERE c > 1;

知道如何实现这一目标吗?我也尝试过不带括号:$columns = array('c'=>'a + b');这也不起作用。

我之前已经尝试过 $this->getAdapter()->query($sqlQuery, Adapter::QUERY_MODE_EXECUTE);但我遇到了无法用 ->closeCursor() 解决的无缓冲查询问题.

最佳答案

使用Zend\Db\Sql\Predicate\Expression:

$columns = array('c'=> new Expression('(ta.a + ta.b)'));

关于php - ZF2 select 中的别名 - 带有计算字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37579814/

相关文章:

php - document.referrer - 限制?

php - Predis 与 laravel 5.5 "No connections available in the pool in Aggregate/RedisCluster.php:337 "

php - 使用 PHP 向日期时间添加一年

MySQL按列从表中选择x条记录

mysql - aws - 如何使用用户的名字或姓氏中的用户字符查询用户?

mysql - 全文搜索字段和 Doctrine 2

php - ZF2 中的通用分页

php - 检索数据库值和 php 文本框值

zend-framework2 - BjyAuthorize 设置文件连接数据库表

mysql - 从表中获取用户的最高分