2011-06-06 60 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