如何在zend 2中编写子查询

时间:2016-07-07 06:15:19

标签: mysql zend-framework2

SELECT
booking_request.book_id,
booking_request.pickup_loc,
booking_request.drop_loc,
booking_request.journey_date,
booking_request.return_date,
booking_request。距离,
booking_request.est_fare,
DvrExp.expenses AS费用,
PndBal.pending AS payment_Pending,
PymtRcvd.receive AS Payment_Recevied,
booking_details.car_id AS car_id,
booking_details.car_name AS car_name,
booking_details.driver_id AS driver_id,
booking_details.driver_name AS driver_name
来自booking_request
左边加入booking_details
    ON booking_request.book_id = booking_details.book_id
LEFT JOIN(
SELECT book_id,
SUM(金额)AS费用
来自driver_expense GROUP BY book_id)DvrExp
ON DvrExp.book_id = booking_details.book_id
LEFT JOIN(
选择book_id,
SUM(amount_pending)AS待定
从FROMIPP平衡GROUP BY book_id)PndBal
ON PndBal.book_id = booking_details.book_id
LEFT JOIN(
选择book_id,
SUM(金额)AS收到
来自payment_receive GROUP BY book_id)PymtRcvd
ON DvrExp.book_id = booking_details.book_id
在哪里booking_request.status ='已关闭'

3 个答案:

答案 0 :(得分:1)

请参阅我在Github上创建的要点的最后一个例子(#17 - 从第268行开始): https://gist.github.com/andrewscaya/8b7d53cffd7c1cb61c234c0de126e964

供将来参考:

    use Zend\Db\Sql\Select;
    use Zend\Db\Sql\Where;
    use Zend\Db\Sql\Expression;

    $sqlExpression = new Expression('MAX(`sku`)');
    $subSelect = new Select();
    $subSelect->from('purchases')
            ->columns([$sqlExpression]);
    $where = new Where();
    $where->in('sku', ['sku' => $subSelect]);
    $select = new Select();
    $select->from('products');
    $select->where($where);

答案 1 :(得分:0)

缺少主要选择条款中的booking_request

    SELECT booking_request.book_id
    ,booking_request.pickup_loc
    ,booking_request.drop_loc
    ,booking_request.journey_date
    ,booking_request.return_date
    ,booking_request.distance
    ,booking_request.est_fare
    ,DvrExp.expenses AS expenses
    ,PndBal.pending AS payment_Pending
    ,PymtRcvd.receive AS Payment_Recevied
    ,booking_details.car_id AS car_id
    ,booking_details.car_name AS car_name
    ,booking_details.driver_id AS driver_id
    ,booking_details.driver_name AS driver_nameFROMbooking_request
FROM booking_request
LEFT JOIN booking_details ON booking_request.book_id = booking_details.book_idLEFT
JOIN (
    SELECT book_id
        ,SUM(amount) AS expenses
    FROM driver_expense
    GROUP BY book_id
    ) DvrExp ON DvrExp.book_id = booking_details.book_id
LEFT JOIN (
    SELECT book_id
        ,SUM(amount_pending) AS pending
    FROM payment_balance
    GROUP BY book_id
    ) PndBal ON PndBal.book_id = booking_details.book_id
LEFT JOIN (
    SELECT book_id
        ,SUM(amount) AS receive
    FROM payment_receive
    GROUP BY book_id
    ) PymtRcvd ON DvrExp.book_id = booking_details.book_id
WHERE booking_request.STATUS = 'Closed'

答案 2 :(得分:0)

$select = $this->getSql()->select();
    $select->join('booking_details','booking_request.book_id = booking_details.book_id',array('car_id', 'car_name','driver_id', 'driver_name'),"LEFT");         


    $select->join(array('DvrExp' => '(SELECT book_id, SUM(amount) AS expenses FROM driver_expense GROUP BY book_id)'),'DvrExp.book_id = booking_details.book_id',array('expenses'),"LEFT");
    $select->join(array('PndBal' => '(SELECT book_id, SUM(amount_pending) AS pending FROM payment_balance  GROUP BY book_id)'),'PndBal.book_id = booking_details.book_id',array('pending'),"LEFT");
    $select->join(array('PymtRcvd' => '(SELECT book_id, SUM(amount) AS receive FROM payment_receive GROUP BY book_id)'),'PymtRcvd.book_id = booking_details.book_id ',array('receive'),"LEFT");     
    $select->where("booking_request.status = '".$booking_type."'" );
    $this->_set_date_filter($select,$params);
    $select->where("booking_request.request_date > '2016-06-07 00:00:00'" );    

    //echo $select->getSqlString(); die;
    $resultSet = $this->selectWith($select);        
    return $resultSet;