【问题标题】:Alias in ZF2 select - with calculated fieldZF2 选择中的别名 - 带有计算字段
【发布时间】:2016-06-01 22:29:11
【问题描述】:

我想用 ZF2 创建以下查询:

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

一个表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() 无法解决的无缓冲查询问题。

【问题讨论】:

    标签: php mysql zend-framework2 php-5.5


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      相关资源
      最近更新 更多