2014-10-08 65 views
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())); 

但後來我不知道如何把這樣的選擇到集合中。有人能幫助我嗎?

回答

2

您可以嘗試使用下面的變體來進行內Magento的UNION語句:

$collection = Mage::getModel('sales/order')->getCollection(); 

$select1 = clone $collection->getSelect(); 
$select1->joinRight... 

$select2 = clone $collection->getSelect(); 
$select2->joinRight... 

$collection->getSelect()->reset(); 
$collection->getSelect()->union(array($select1, $select2)); 
0

我不能完全得到阿列克謝的方法來上班,是的,但這個工作對我來說:

$selectOne = clone $this; 
$selectOne->addFieldToFilter(...); 
$selectOne->addFieldToFilter(...); 
$selectOne->getSelect()->limit(...); 
$selectOneSql = $selectOne->getSelect()->__toString(); 

$selectTwo = clone $this; 
$selectTwo->getSelect()->setPart('where', null); 
$selectTwo->addFieldToFilter(...); 
$selectTwo->addFieldToFilter(...); 
$selectTwo->getSelect()->limit(...); 
$selectTwoSql = $selectTwo->getSelect()->__toString(); 

$this->getSelect()->reset(); 
$this->getSelect()->union(array(
    new Zend_Db_Expr($selectOneSql), 
    new Zend_Db_Expr($selectTwoSql), 
)); 

從我所知道的,大多數或所有在Magento核心中使用->union()的地方,方法都是傳遞字符串或Zend_Db_Expr對象。