【问题标题】:How to use join in magento to get colloection如何在magento中使用join来获取收藏
【发布时间】:2016-03-03 22:59:31
【问题描述】:

我需要将下面的 sql 查询转换成 magento 来获取集合。

SELECT * FROM sales_flat_order LEFT JOIN sales_flat_order_address ON sales_flat_order.entity_id=sales_flat_order_address.parent_id And sales_flat_order_address.store_id!=x;

我试过这个,我知道这是错误的,只是想知道我想做什么,其中“555”是 sql 查询中的 x

 $ordercollection = Mage::getModel('sales/order')->getCollection();
    $ordercollection->getSelect()->joinLeft(array('sfoa' => 'sales_flat_order_address'),'main_table.entity_id = sfoa.parent_id',array(,=>'sfoa.store_id'));
    $ordercollection->addFieldToFilter('store_id',array('neq'=>'555'));

【问题讨论】:

    标签: mysql sql magento filter


    【解决方案1】:

    你快到了:

    $orderCollection = Mage::getModel('sales/order')->getCollection();
    $orderCollection->getSelect()->joinLeft(array('sfoa' => 'sales_flat_order_address'),'main_table.entity_id = sfoa.parent_id', "");
    $orderCollection->addFieldToFilter('main_table.store_id',array('neq'=>'555'));
    

    输出

    SELECT `main_table`.* FROM `sales_flat_order` AS `main_table` LEFT JOIN `sales_flat_order_address` AS `sfoa` ON main_table.entity_id = sfoa.parent_id WHERE (main_table.store_id != '555')
    

    SFOA 不包含 store_id 列,因此我更新了上面的集合查询。

    foreach($orderCollection as $collection) {
        // do something
        //var_dump($collection->getData());die();
    }
    

    【讨论】:

    • 这不返回任何东西
    • 这在 db 上运行良好,在磁电机中它不是 'Mage::getModel('sales/order')->getCollection(); $ordercollection->addFieldToFilter('store_id',array('neq'=>'555')); $ordercollection->getSelect()->joinLeft('sales_flat_order_address', "main_table.entity_id = sales_flat_order_address.parent_id");'
    • 如果你 $ordercollection->getSelect()->__toString() 这和你的 SQL 比较吗?
    • SELECT main_table.*, sales_flat_order_address.* FROM sales_flat_order AS main_table LEFT JOIN sales_flat_order_address ON main_table.entity_id = sales_flat_order_address.parent_id WHERE (store_id != '555')
    • 以上是我之前评论的sql,它给出了我想要的,你可以在这个链接的cmets中看到stackoverflow.com/questions/33993812/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多