2
我正在使用Magento,並且在使用sql命令UNION和我的集合時遇到了問題。我有兩個集合:Magento UNION SQL
$collection = Mage::getModel('sales/order')->getCollection();
$collection->getSelect()->joinRight(
array('sfi' => 'sales_flat_invoice'),
'main_table.entity_id = sfi.order_id AND
NOW()<= DATE_ADD(main_table.created_at, INTERVAL 3 MONTH)',
array()
);
$collection->getselect()->joinLeft(
array('ztr'=>'zdg_tv_report'),
'ztr.type=\'TV\' AND ztr.number=main_table.increment_id',
array('*')
);
$collection->addFieldToSelect(new Zend_Db_Expr('main_table.increment_id'));
和
$collection2 = Mage::getModel('sales/order')->getCollection();
$collection2->getSelect()->joinRight(
array('sfc'=>'sales_flat_creditmemo'),
'main_table.entity_id= sfc.order_id AND
NOW()<= DATE_ADD(main_table.created_at, INTERVAL 3 MONTH)',
array()
);
$collection2->getSelect()->joinLeft(
array('ztr'=>'zdg_tv_report'),
'ztr.type=\'Reverse_TV\' AND ztr.number=main_table.increment_id',
array('*')
);
$collection2->addFieldToSelect(new Zend_Db_Expr('main_table.increment_id'));
我試圖用
$collection->getSelect()->union(array($collection2->getSelect()));
但SQL選擇我似乎是完全搞砸了:
SELECT main_table.increment_id, `ztr`.*SELECT main_table.increment_id, `ztr`.* FROM `sales_flat_order` AS `main_table` RIGHT JOIN `sales_flat_creditmemo` AS `sfc` ON main_table.entity_id= sfc.order_id AND NOW()<= DATE_ADD(main_table.created_at, INTERVAL 3 MONTH) LEFT JOIN `zdg_tv_report` AS `ztr` ON ztr.type='Reverse_TV' AND ztr.number=main_table.increment_id FROM `sales_flat_order` AS `main_table` RIGHT JOIN `sales_flat_invoice` AS `sfi` ON main_table.entity_id = sfi.order_id AND NOW()<= DATE_ADD(main_table.created_at, INTERVAL 3 MONTH) LEFT JOIN `zdg_tv_report` AS `ztr` ON ztr.type='TV' AND ztr.number=main_table.increment_idSELECT main_table.increment_id, `ztr`.*SELECT main_table.increment_id, `ztr`.* FROM `sales_flat_order` AS `main_table` RIGHT JOIN `sales_flat_creditmemo` AS `sfc` ON main_table.entity_id= sfc.order_id AND NOW()<= DATE_ADD(main_table.created_at, INTERVAL 3 MONTH) LEFT JOIN `zdg_tv_report` AS `ztr` ON ztr.type='Reverse_TV' AND ztr.number=main_table.increment_id FROM `sales_flat_order` AS `main_table` RIGHT JOIN `sales_flat_invoice` AS `sfi` ON main_table.entity_id = sfi.order_id AND NOW()<= DATE_ADD(main_table.created_at, INTERVAL 3 MONTH) LEFT JOIN `zdg_tv_report` AS `ztr` ON ztr.type='TV' AND ztr.number=main_table.increment_id
我也嘗試使用
$unionSelect = new Varien_Db_Select();
$unionSelect->union(array($collection->getSelect(), $collection2->getSelect()));
但後來我不知道如何把這樣的選擇到集合中。有人能幫助我嗎?