获取php中具有相同id的字段的最大值

时间:2015-05-12 06:38:54

标签: php mysql zend-framework zend-framework2 zend-db-table

我需要返回所有InstallmentDate以及返回数据,这里的问题是我对多个OrderInstallmentDetailsId具有相同的InstallmentDate

    $select = $this->select()
            ->setIntegrityCheck(false)
            ->from(array('j' => DB_TABLE_PREFIX . 'finance_journal'), array(
                '*',
                'JournalCreatedDate' => 'CreatedDate'
            ))
            ->join(array('jr' => DB_TABLE_PREFIX . 'finance_journal_reference'), 'j.JournalId = jr.JournalId', array(
                'totalDebitAmount' => 'SUM(DebitAmount)',
                'totalCreditAmount' => 'SUM(CreditAmount)',
                'debitBalanceAmount' => 'SUM(DebitAmount) - SUM(CreditAmount)',
            ))
            ->join(array('a' => DB_TABLE_PREFIX . 'finance_account'), 'jr.AccountId = a.AccountId', array(
                'AccountId',
                'AccountGroupId',
                'AccountCode',
                'AccountPath',
                'AccountName'
            ))
            ->join(array('ja' => DB_TABLE_PREFIX . 'finance_journal_approved_user'), 'j.JournalId = ja.JournalId', array())
            ->join(array('c' => DB_TABLE_PREFIX . 'customer'), 'a.AccountId = c.AccountId', array(
                'CustomerId',
                'CustomerAccountId' => 'AccountId'
            ))
            ->join(array('O' => DB_TABLE_PREFIX . 'order'), 'O.UserId = c .CustomerId', array(
                'OrderId'
               ,'OrderInstallmentId'))
             ->join(array('oi' => DB_TABLE_PREFIX . 'order_installment'), 'oi.OrderInstallmentId = O.OrderInstallmentId', array(
                'OrderInstallmentId',
                'InstallmentId',
                'NumberOfInstallments'
            ))
             ->join(array('oid' => DB_TABLE_PREFIX . 'order_installment_details'), 'oid.OrderInstallmentId = oi.OrderInstallmentId', array(
                'OrderInstallmentDetailsId',
                'InstallmentDate'
            ))
            ->join(array('f' => DB_TABLE_PREFIX . 'firm'), 'f.FirmId = c.FirmId', array('FirmName'))
            ->join(array('u' => DB_TABLE_PREFIX . 'user'), 'u.UserId = c.CustomerId', array('FirstName', 'LastName'))
            ->group('a.AccountId')
         ->order('j.CreatedDate ASC');
           //->order('oid.OrderInstallmentDetailsId DESC');

    if ($this->_authUser->sessionBranch() > 0) {
        $select->where('j.BranchId = ?', $this->_authUser->sessionBranch());
    }

    if ($firmId > 0) {
        $select->where('f.FirmId = ?', $firmId);
    }
    if (empty($fromDate)) {
        $fromDate = strtotime('-30 days');
    } else {
        $select->where('j.JournalDate >= ?', Utils_Converter::mysqlDate($fromDate));
    }

    if (!empty($toDate)) {
        $select->where('j.JournalDate <= ?', Utils_Converter::mysqlDate($toDate));
    }

    if ($minimumDebitBalance == 0) {
        $select->having('debitBalanceAmount > ?', $minimumDebitBalance);
    } else {
        $select->having('debitBalanceAmount >= ?', $minimumDebitBalance);
    }

    if ($maximumDebitBalance == 0) {            
    } else {
        $select->having('debitBalanceAmount < ?', $maximumDebitBalance);
    }

    //echo $select;exit;
    $returnData = array();
    foreach ($this->fetchAll($select) as $row) {
        //if($row['debitBalanceAmount'] >= $minimumDebitBalance){
        $returnData[$row['AccountId']] = $row->toArray();
        //}
    } 
    return $returnData;
}

我的主要目标是从日期数组中获取最大日期。

1 个答案:

答案 0 :(得分:1)

    ->join(array('oid' => DB_TABLE_PREFIX . 'order_installment_details'), 'oid.OrderInstallmentId = oi.OrderInstallmentId', array(
                'OrderInstallmentDetailsId',
                'lastInstallmentDate' => new Zend_Db_Expr('MAX(InstallmentDate)')
            ))
            ->join(array('f' => DB_TABLE_PREFIX . 'firm'), 'f.FirmId = c.FirmId', array('FirmName'))
            ->join(array('u' => DB_TABLE_PREFIX . 'user'), 'u.UserId = c.CustomerId', array('FirstName', 'LastName'))
            ->group('a.AccountId')
         ->order('j.CreatedDate ASC');