Trước hết, những gì tôi đang cố gắng làm:
Nhận một bộ sưu tập với những khách hàng chưa có đơn đặt hàng và những người đăng ký mới đăng ký không có tài khoản khách hàng.
Trong mysql FULL THAM GIA và FULL OUTER THAM GIA không tồn tại nên truy vấn sql cơ bản của tôi là:
SELECT
`e`.`entity_id`, `e`.`group_id`, `e`.`email`, `salesOrder`.`entity_id` AS `order_id`, `salesRecurringProfile`.`profile_id`, `newsletterSubscriber`.`subscriber_status`, `newsletterSubscriber`.`subscriber_id`, `newsletterSubscriber`.`subscriber_email`, `newsletterSubscriber`.`customer_id`
FROM
`customer_entity` AS `e`
LEFT JOIN
`sales_flat_order` AS `salesOrder` ON e.entity_id = salesOrder.customer_id AND salesOrder.status = 'processing'
LEFT JOIN
`sales_recurring_profile` AS `salesRecurringProfile` ON e.entity_id = salesRecurringProfile.customer_id AND salesRecurringProfile.state = 'active'
LEFT JOIN
`newsletter_subscriber` AS `newsletterSubscriber` ON e.entity_id = newsletterSubscriber.customer_id WHERE (salesOrder.entity_id IS NULL AND salesRecurringProfile.profile_id IS NULL)
UNION
SELECT
`e`.`entity_id`, `e`.`group_id`, `e`.`email`, `salesOrder`.`entity_id` AS `order_id`, `salesRecurringProfile`.`profile_id`, `newsletterSubscriber`.`subscriber_status`, `newsletterSubscriber`.`subscriber_id`, `newsletterSubscriber`.`subscriber_email`, `newsletterSubscriber`.`customer_id`
FROM
`customer_entity` AS `e`
LEFT JOIN
`sales_flat_order` AS `salesOrder` ON e.entity_id = salesOrder.customer_id AND salesOrder.status = 'processing'
LEFT JOIN
`sales_recurring_profile` AS `salesRecurringProfile` ON e.entity_id = salesRecurringProfile.customer_id AND salesRecurringProfile.state = 'active'
RIGHT JOIN
`newsletter_subscriber` AS `newsletterSubscriber` ON e.entity_id = newsletterSubscriber.customer_id WHERE (salesOrder.entity_id IS NULL AND salesRecurringProfile.profile_id IS NULL)
Tôi đang cố gắng thay đổi điều này bằng cách sử dụng các bộ sưu tập để sử dụng nó với lưới:
$customersSubscribersLeftJoin = Mage::getResourceModel('customer/customer_collection');
$customersSubscribersLeftJoin->getSelect()
->reset(Zend_Db_Select::COLUMNS)
->columns(array(
'entity_id',
'group_id',
'email'
))
->joinLeft(array('salesOrder' => $resource->getTableName('sales/order')), 'e.entity_id = salesOrder.customer_id AND salesOrder.status = \'' . Mage_Sales_Model_Order::STATE_PROCESSING . '\'', array(
'entity_id' => 'salesOrder.entity_id AS order_id'
))
->joinLeft(array('salesRecurringProfile' => $resource->getTableName('sales/recurring_profile')), 'e.entity_id = salesRecurringProfile.customer_id AND salesRecurringProfile.state = \'' . Mage_Sales_Model_Recurring_Profile::STATE_ACTIVE . '\'', array(
'profile_id' => 'profile_id'
))
->joinLeft(array('newsletterSubscriber' => $resource->getTableName('newsletter/subscriber')), 'e.entity_id = newsletterSubscriber.customer_id', array(
'subscriber_status' => 'subscriber_status',
'subscriber_id' => 'subscriber_id',
'subscriber_email' => 'subscriber_email',
'customer_id' => 'customer_id'
))
->reset(Zend_Db_Select::WHERE)
->where('salesOrder.entity_id IS NULL AND salesRecurringProfile.profile_id IS NULL');
$customersSubscribersRightJoin = Mage::getResourceModel('customer/customer_collection');
$customersSubscribersRightJoin->getSelect()
->reset(Zend_Db_Select::COLUMNS)
->columns(array(
'entity_id',
'group_id',
'email'
))
->joinLeft(array('salesOrder' => $resource->getTableName('sales/order')), 'e.entity_id = salesOrder.customer_id AND salesOrder.status = \'' . Mage_Sales_Model_Order::STATE_PROCESSING . '\'', array(
'entity_id' => 'salesOrder.entity_id AS order_id'
))
->joinLeft(array('salesRecurringProfile' => $resource->getTableName('sales/recurring_profile')), 'e.entity_id = salesRecurringProfile.customer_id AND salesRecurringProfile.state = \'' . Mage_Sales_Model_Recurring_Profile::STATE_ACTIVE . '\'', array(
'profile_id' => 'profile_id'
))
->joinRight(array('newsletterSubscriber' => $resource->getTableName('newsletter/subscriber')), 'e.entity_id = newsletterSubscriber.customer_id', array(
'subscriber_status' => 'subscriber_status',
'subscriber_id' => 'subscriber_id',
'subscriber_email' => 'subscriber_email',
'customer_id' => 'customer_id'
))
->reset(Zend_Db_Select::WHERE)
->where('salesOrder.entity_id IS NULL AND salesRecurringProfile.profile_id IS NULL');
Vậy làm thế nào tôi có thể tạo một UNION của 2 bộ sưu tập đó, hoặc thực hiện nó trong một cuộc gọi duy nhất chỉ với bộ sưu tập đầu tiên?
Cảm ơn bạn trước
BIÊN TẬP :
Bằng cách sử dụng :
$customersSubscribersLeftJoin->getSelect()
->union(
array(
$customersSubscribersRightJoin->getSelect()
)
);
Truy vấn đã cho là:
SELECT
`e`.`entity_id`, `e`.`group_id`, `e`.`email`, `salesOrder`.`entity_id` AS `order_id`, `salesRecurringProfile`.`profile_id`, `newsletterSubscriber`.`subscriber_status`, `newsletterSubscriber`.`subscriber_id`, `newsletterSubscriber`.`subscriber_email`, `newsletterSubscriber`.`customer_id`SELECT `e`.`entity_id`, `e`.`group_id`, `e`.`email`, `salesOrder`.`entity_id` AS `order_id`, `salesRecurringProfile`.`profile_id`, `newsletterSubscriber`.`subscriber_status`, `newsletterSubscriber`.`subscriber_id`, `newsletterSubscriber`.`subscriber_email`, `newsletterSubscriber`.`customer_id`
FROM
`customer_entity` AS `e`
LEFT JOIN
`sales_flat_order` AS `salesOrder` ON e.entity_id = salesOrder.customer_id AND salesOrder.status = 'processing'
LEFT JOIN
`sales_recurring_profile` AS `salesRecurringProfile` ON e.entity_id = salesRecurringProfile.customer_id AND salesRecurringProfile.state = 'active'
RIGHT JOIN
`newsletter_subscriber` AS `newsletterSubscriber` ON e.entity_id = newsletterSubscriber.customer_id
WHERE
(salesOrder.entity_id IS NULL AND salesRecurringProfile.profile_id IS NULL)
FROM
`customer_entity` AS `e`
LEFT JOIN
`sales_flat_order` AS `salesOrder` ON e.entity_id = salesOrder.customer_id AND salesOrder.status = 'processing'
LEFT JOIN
`sales_recurring_profile` AS `salesRecurringProfile` ON e.entity_id = salesRecurringProfile.customer_id AND salesRecurringProfile.state = 'active'
LEFT JOIN
`newsletter_subscriber` AS `newsletterSubscriber` ON e.entity_id = newsletterSubscriber.customer_id
WHERE
(salesOrder.entity_id IS NULL AND salesRecurringProfile.profile_id IS NULL)
Đó rõ ràng không phải là những gì chúng ta muốn bằng cách làm cho sự kết hợp của hai truy vấn, bất kỳ ý tưởng?