2011-06-06 80 views
0

我試圖轉換以下MySQL查詢,這些查詢給了我六個月或更長時間沒有約會的所有客戶進入DQL查詢。幫助需要將MySQL查詢轉換爲原則查詢

mysql> select appt.lastDate, clients.firstname, clients.lastname 
-> from (select max(gapmtDate) as lastDate,gapmtClient from groomappointments group by gapmtClient) as appt ,clients 
-> where date_sub(CURDATE(), INTERVAL 6 MONTH)>lastDate 
-> AND clients.clientid = appt.gapmtClient; 

我試圖將它與以下內容匹配,但是我收到一個錯誤,指出where子句中存在未知列g__0。任何人都可以指出我要去哪裏錯了

public function noappointmentsforsixmonthsAction() 
{ 



    $sixmonths=date('y-m-d',strtotime("-6 months")); 

    $q=Doctrine_Query::create() 
    ->select('MAX(g.gapmtDate) AS lastDate, c.firstname, c.lastname,g.gapmtClient') 
->from('PetManager_Model_Groomappointments g') 
->leftJoin('g.PetManager_Model_Clients c') 
->where('lastDate <?',$sixmonths) 
->groupBy('g.gapmtClient'); 

....... 

} 

編輯

好似乎主義網站被降了一整天我已經試過編輯查詢到低於但它仍然是行不通的。任何人都可以給我正確的語法。

$q=Doctrine_Query::create() 
->select('a.lastDate, c.firstname, c.lastname') 
->addSelect('(SELECT MAX(gapmtDate) AS lastDate, gapmtClient FROM PetManager_Model_Groomappointments GROUP BY gapmtClient) as a') 
->from('PetManager_Model_Groomappointments a') 
->leftJoin('a.PetManager_Model_Clients c') 
->where('a.gapmtClient=c.clientID') 
->andWhere('lastDate >?',$sixmonths); 

回答

0

試試這個:

$q=Doctrine_Query::create() 
    ->select('MAX(g.gapmtDate) AS lastDate, c.firstname, c.lastname, g.gapmtClient') 
->from('PetManager_Model_Groomappointments g') 
->leftJoin('g.PetManager_Model_Clients c') 
->where('MAX(g.gapmtDate) < ?', $sixmonths) 
->groupBy('g.gapmtClient'); 

我希望這個作品。

+0

感謝您的回覆,但MySQL不允許在where子句中使用聚合函數。 – Graham 2011-06-06 18:39:00

+0

@Graham你確定嗎?我現在在一個mysql WHERE子句中使用了COUNT。 – Garry 2011-06-07 06:39:24

+0

我相信這是正確的從我讀過http://forums.mysql.com/read.php?10,193505,193505#msg-193505 http://lists.mysql.com/mysql/164529。這很可能是我的查詢格式。我曾嘗試過你的格式,並且出現了關於聚合函數無效使用的錯誤 – Graham 2011-06-07 13:49:27