dongzhiyi2006 2012-11-28 13:08
浏览 94

使用SUM的ZF2自定义数据库查询

Can someone give me a hint what I am doing wrong?

public function getPaymentSumByTypeAndProject($project_id,$type) {
  $type = (int) $type;
  $project_id = (int) $project_id;
  $rowset = $this->tableGateway->select(array('total_amount' => new Expression('SUM(payment.amount)')))->where(array('type' => $type, 'project_id' => $project_id));
  $row = $rowset->toArray();
  if (!$row) {
    throw new \Exception("Busted :/");
  }
  return $rowset;
}

I want to make the same query:

SELECT SUM(amount) FROM payment WHERE type='$type' AND project_id ='$project_id';

Edit: I made small progress, i have figured out how to sum whole column

public function getPaymentSumByTypeAndProject($project_id, $type) {
    $type = (int) $type;
    $project_id = (int) $project_id;
    $resultSet = $this->tableGateway->select(function (Select $select) {
                $select->columns(array(new \Zend\Db\Sql\Expression('SUM(amount) as amount')))->where('type="0"');
            });
    return $resultSet;

Maybe someone could help me to figure out how to add condition: "WHERE type='$type' AND project_id='$project_id'" ?

  • 写回答

4条回答 默认 最新

  • doujiebo9849 2012-11-28 13:13
    关注

    try to make like that instead of (int) $type

     intval($type);
    

    and

      intval($project_id);
    

    and in your sql change your variables to

       '".$type."' 
    

    AND

      '".$project_id."'
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?