2015-05-12 61 views
0

我需要返回所有InstallmentDate以及返回數據,這裏的問題是我對於多個InstallmentDate s有相同的OrderInstallmentDetailsId在php中獲取具有相同ID的字段的最大值

$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
->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'); 
+0

自己找到了。 –

相關問題