Zend中的嵌套查询

时间:2012-01-02 09:24:42

标签: zend-framework

请告诉我如何在ZEND中编写此查询。

Query= select p.project_id,paw.project_name 
       from lifecycle l,project_delivery_center pdc,project p,
(select distinct project_id,max(lifecycle_id) as lifecycle_id from project_lifecycle group by project_id) npl,project_after_win paw 
       where p.project_id=npl.project_id and p.project_id=paw.project_id and npl.lifecycle_id=l.lifecycle_id and l.phase_id>=3 and pdc.project_id=p.project_id and pdc.delivery_center_id=".$_SESSION['dcId']." order by paw.project_name

1 个答案:

答案 0 :(得分:0)

假设您使用ZEND表示Zend_Db,这应该是正确的:

$subSelect = $db->select();
$subSelect->from('project_lifecycle', array(
        'project_id',
        'lifecycle_id' => 'MAX(lifecycle_id)',
    ))
    ->group('project_id');

$select = $db->select();
$select->from(array('l' => 'lifecycle'), null)
    ->join(array('npl' => $subSelect), null)
    ->join(array('pdc' => 'project_delivery_center'),
           'pdc.project_id = p.project_id', null)
    ->join(array('p' => 'project'), 'p.project_id = npl.project_id',
           array('p.project_id'))
    ->join(array('paw' => 'project_after_win'),
           'paw.project_id = p.project_id', array('paw.project_name'))
    ->where('npl.lifecycle_id = l.lifecycle_id')
    ->where('l.phase_id >= ?', 3)
    ->where('pdc.delivery_center_id = ?', $_SESSION['dcId'])
    ->order('paw.project_name');