douxun3496 2014-07-08 11:22
浏览 28
已采纳

magento中的concat sql查询

I am new in magento. I am writing a code like:

   $products = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToSort("entity_id", "DESC")
                ->addAttributeToSelect(array('name', 'price', 'small_image'))
                ->setVisibility ( Mage::getSingleton( 'catalog/product_visibility')->getVisibleInSiteIds())
                ->setOrder($this->get_order(), $this->get_order_dir());

then again when using condition, by using the value of $products I am writing the code like:

if($size != ''){
    $products->getSelect()
    ->joinLeft(array('cat_product' => 'catalog_category_product'), 'cat_product.product_id=e.entity_id')
    ->joinLeft(array("at_int" => 'catalog_product_entity_int'), 'cat_product.product_id=at_int.entity_id')
    ->joinLeft(array('cpf'=>'catalog_product_flat_1'),'cpf.entity_id=e.entity_id',array('shhirt_size'))
    ->where('at_int.value=30 AND cpf.shhirt_size='.$size.' AND at_int.attribute_id=(SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="product_attrb_type_name") ')
    ;
}else if($size == ''){
    $products->getSelect()
    ->joinLeft(array('cat_product' => 'catalog_category_product'), 'cat_product.product_id=e.entity_id')
    ->joinLeft(array("at_int" => 'catalog_product_entity_int'), 'cat_product.product_id=at_int.entity_id')
    ->where('at_int.value=30 AND at_int.attribute_id=(SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code`="product_attrb_type_name") ' )
    ;
}

It is working fine. But here code is re-written. How to write the above code such a manner, only where clause would be in if part and else part.

Please help me. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • duan198727 2014-07-08 12:33
    关注

    I used solution of Haim Evgi and slightly corrected it

    
        $subSelect = clone $products->getSelect();
        $subSelect->reset()
            ->from($products->getTable('eav_attribute'), array('attribute_id'))
            ->where('`attribute_code`="product_attrb_type_name"');
    
        $q = $products->getSelect()
            ->joinLeft(array('cat_product' => 'catalog_category_product'), 'cat_product.product_id=e.entity_id')
            ->joinLeft(array("at_int" => 'catalog_product_entity_int'), 'cat_product.product_id=at_int.entity_id')
            ->where('at_int.value=30')
            ->where('at_int.attribute_id=?', new Zend_Db_Expr(sprintf('(%s)', $subSelect)));
        if($size != ''){    
            $q->joinLeft(array('cpf'=>'catalog_product_flat_1'), 'cpf.entity_id=e.entity_id', array('shhirt_size'))
                ->where('cpf.shhirt_size=?', $size);
        }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog