zend框架查询

时间:2011-05-08 10:11:37

标签: php sql zend-framework

有些人可以帮我解决一下这个问题。 Basicall我知道如何在Zend Framework中连接表但我无法弄清楚的是最后一部分开始“AND ......”这就是我开始我的子查询的地方。我的查询就像

SELECT requests.`email`,requests.`title`, `request_category`.`request_category_name`,
       `request_route_category`.`department_id`,`departments`.`department_name`
 FROM requests 
 JOIN `request_category` ON requests.`requests_category_id` = request_category.`request_category_id` 
 JOIN `request_route_category` ON `request_category`.`path_id` = `request_route_category`.`path_id` 
 JOIN `departments` ON `request_route_category`.`department_id` = `departments`.`department_id` 
                    AND (SELECT `department_id` 
                         FROM `request_route_category` 
                         WHERE `request_route_category`.`department_id`=`departments`.`department_id` 
                         AND `request_route_category`.`is_complete`=0 
                         LIMIT 1)=7

我现在有这个

$select=$this->select()
        ->setIntegrityCheck(false)
        ->from($this->_name,array('reqemail'=>'email','reqcreated'=>'created'))
        ->join('request_category', 'requests.requests_category_id=request_category.request_category_id',array('catpath'=>'path_id','catid'=>'request_category.request_category_id','catname'=>'request_category_name'))
        ->join('request_route_category', 'request_category.path_id=request_route_category.path_id',array())
        ->join('departments','request_route_category.department_id=departments.department_id');

1 个答案:

答案 0 :(得分:1)

据我所知,你不能使用Zend_DB加入SubQueries,但你可以这样做:

$subQuery = $this->select()
        ->setIntegrityCheck(false)
        ->from('request_route_category')
        ->where('request_route_category.department_id = departments.department_id')
        ->where('request_route_category.is_complete = ?', 0)
        ->limit(1);

$sql = $select=$this->select()
    ->setIntegrityCheck(false)
    ->from($this->_name,array('reqemail'=>'email','reqcreated'=>'created'))
    ->join('request_category', 'requests.requests_category_id=request_category.request_category_id',array('catpath'=>'path_id','catid'=>'request_category.request_category_id','catname'=>'request_category_name'))
    ->join('request_route_category', 'request_category.path_id=request_route_category.path_id',array())
    ->join('departments','request_route_category.department_id=departments.department_id AND ('.$subQuery.')=7');