2014-10-08 98 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对象。