2013-10-30 81 views
0

作爲zend框架中的初學者,我需要一些幫助來編寫sql查詢。 這是我的查詢:在Zend中編寫sql查詢

SELECT 
    COUNT(*) AS Ouvert , SUBSTRING_INDEX(emails.email_value, '@', -1) AS FAI, track_open.open_date 
    FROM emails, track_open 
    WHERE emails.email_id = track_open.email_id 
    AND DATE(open_date) = CURDATE() 
    GROUP BY SUBSTRING_INDEX(emails.email_value, '@', -1) 

這我的函數在那裏我將它寫:

public function getOpen() 
{ 
    $query = $this->select() 
        ->setIntegrityCheck(false) 
        ->from(array('e' => 'emails')) 
        ->join(array('to' => 'track_open'), 'e.email_id = to.email_id') 
       ?????????? 

    $result = $this->fetchAll($query); 

    if($result) 
    { 
     return $result->toArray(); 
    } 

    return false; 
} 

因此,如果任何人都可以舉一個例子來寫這個查詢,因爲我不知道如何完成它。

回答

1

它已經很長一段時間,因爲我已經與Zend_Db的工作,我真的不能測試代碼,但你可以嘗試以下方法:

$query = $this->select() 
       ->setIntegrityCheck(false) 
       ->from(array('e' => 'emails'), array(
        'Ouvert' => new Zend_Db_Expr('COUNT(*)'), 
        'FAI' => new Zend_Db_Expr("SUBSTRING_INDEX(emails.email_value, '@', -1)"), 
        'track_open.open_date', 
      )) 
       ->join(array('to' => 'track_open'), 'e.email_id = to.email_id') 
       ->where('emails.email_id = track_open.email_id') 
       ->where('DATE(open_date) = CURDATE()') 
       ->group(new Zend_Db_Expr("SUBSTRING_INDEX(emails.email_value, '@', -1)"))