如何在zf2中的INNER JOIN ON条件中使用子查询?

时间:2014-08-25 11:49:45

标签: php zend-framework2

我必须在sql查询中实现一个条件,我需要在INNER JOIN ON条件中使用子查询,如下所示:

$select = new Select('booking');
        $select->columns(array('*', new Expression("service_provider.first_name as sp_first_name, service_provider.last_name as sp_last_name,
        invoice.status_id AS invoice_status,payment_history.currency as currency,CASE invoice.status_id WHEN 0 THEN 'Unpaid' WHEN 1 THEN 'Paid' WHEN 2 THEN 'Partially Paid' END AS PaymentStatus")));
        $select->join('booking_suggestion_history', 'booking_suggestion_history.booking_id = booking.id AND booking_suggestion_history.id = (SELECT id FROM booking_suggestion_history WHERE booking_id = booking.id ORDER BY id DESC LIMIT 1)', 'inner');
        $select->join('users', 'users.id = booking.user_id', array('first_name', 'last_name'), 'left');
        $select->join(array('service_provider' => 'users'), 'service_provider.id = booking.service_provider_id', array(), 'left');
        $select->join('service_provider_service', 'service_provider_service.id = booking.service_provider_service_id', array('duration', 'price'), 'left');
        $select->join('service_category', 'service_category.id = service_provider_service.service_id', array('category_name'), 'left');
        $select->join('invoice', 'invoice.id = booking.invoice_id', array('invoice_total', 'site_commision'), 'inner');
        $select->join('invoice_details', 'invoice_details.invoice_id = invoice.id', array('sale_item_details'), 'inner');
        $select->join('payment_history', 'payment_history.invoice_id = invoice.id', array(), 'inner');
        $select->join('lookup_status', 'lookup_status.status_id = booking.status_id', array('status'), 'left');

查询通过这些条件生成(通过str_replace(''','',$ select-> getSqlString());)工作正常sql但是zf2抛出错误:

Statement could not be executed (42000 - 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`id` `FROM` `booking_suggestion_history` `WHERE` `booking_id` = `booking`.`id` `' at line 2)

请帮助.. !!

0 个答案:

没有答案