简体   繁体   English

Magento联接查询使用3表查询

[英]Magento join query query using 3 table

I am new to magento. 我是magento的新手。 I just want to frame a query, but i am struggling to do. 我只想框架查询,但我在努力。

My mysql query: 我的mysql查询:

  SELECT `main_table`.*, `t1`.*, count(t2.review_id) AS `totalrecord`,    SUM(t2.rating) AS `totalrating`, `t2`.* FROM `nbmp_vendor` AS `main_table` 
LEFT JOIN `customer_entity` AS `t1` ON main_table.customer_id = t1.entity_id 
 LEFT JOIN `nbmp_review` AS `t2` ON main_table.customer_id = t2.vendor_id 
 WHERE ((vendor_status = '1')) AND (t1.group_id = '4') group by main_table.vendor_id  

I have tried in magento: 我在magento中尝试过:

    <?php 
  class Blazedream_TopVendors_Block_Monblock extends Mage_Core_Block_Template {

 public function methodblock() {

    $myTable = "customer_entity";
    $myTable1 = "nbmp_review";
    $collection  = Mage::getModel('topvendors/topvendors')->getCollection()
                            ->addFieldToFilter(array('vendor_status'), array('1'))
                            ->setOrder('vendor_id','asc');

    $collection->getSelect()
            ->joinLeft(array("t1" => $myTable), "main_table.customer_id = t1.entity_id")
            ->where("t1.group_id = '4'");

    $collection->getSelect()
            ->columns('count(t2.review_id) AS totalrecord')
            ->columns('SUM(t2.rating) AS totalrating')
            ->joinLeft(array("t2" => $myTable1), "main_table.customer_id = t2.vendor_id")
            ->group("main_table.vendor_id");
    echo $collection->getselect(); die;
        $ddata = $collection->getData();
       $i = 0;
       foreach($ddata as $data)
       {

       $retour[$i]['id'] = $data->getData('vendor_id');
       $retour[$i]['name'] = $data->getData('vendor_name');
       $retour[$i]['vendor_slug'] = $data->getData('vendor_slug');
       // $retour[$i]['rating_avg'] = $vendors[$data->getData('vendor_id')]['rating_avg'];
       $retour[$i]['totalrating'] = $data->getData('totalrating');
       $retour[$i]['rating_avg'] = $data->getData('totalrating') / $data->getData('totalrecord');
       $i++;

     }
    // Mage::getSingleton('adminhtml/session')->addSuccess('Cool Ca marche !!');
   return $retour;
}
}

I am getting only error. 我只有错误。

 Fatal error: Call to undefined method Blazedream_TopVendors_Model_Mysql4_TopVendors_Collection::group() in magento

Can anyone help me to form this?? 谁能帮我形成这个? Thanks in advance. 提前致谢。

Please try following and see if it helps you. 请尝试关注并查看是否对您有帮助。

$myTable     = "customer_entity";
$myTable1    = "nbmp_review";
$collection  = Mage::getModel('topvendors/topvendors')->getCollection()
                        ->addFieldToFilter(array('vendor_status'), array('1'))
                        ->setOrder('vendor_id','asc');                        

$collection->getSelect()->group('vendor_id'); // TRY THIS LINE OF CODE

$collection->getSelect()
        ->joinLeft(array("t1" => $myTable), "main_table.customer_id = t1.entity_id")
        ->where("t1.group_id = '4'");

$collection->getSelect()
        ->columns('count(t2.review_id) AS totalrecord')
        ->columns('SUM(t2.rating) AS totalrating')
        ->joinLeft(array("t2" => $myTable1), "main_table.customer_id = t2.vendor_id");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM