【问题标题】:convert Raw SQL to Zend_Db_Select将原始 SQL 转换为 Zend_Db_Select
【发布时间】:2014-09-14 20:44:44
【问题描述】:

我需要将此 sql 查询转换为 Zend_Db_Select 对象

'SELECT `main_table`.*, `product_name_table`.`name` as `product_name`, `product_price_table`.`price` as `product_price`,
    COUNT(main_table.answer_id) AS `answer_count`, 
    (SELECT CONCAT(main_table.answer_title, ":::", GROUP_CONCAT(DISTINCT main_table.query SEPARATOR "###"))) AS `answer_title_with_query` 
FROM `nanorepwidgets_answer` AS `main_table`
LEFT JOIN (SELECT `e`.`entity_id`, `at_name`.`value` AS `name` 
        FROM `catalog_product_entity` AS `e` 
        INNER JOIN `catalog_product_entity_varchar` AS `at_name` 
            ON (`at_name`.`entity_id` = `e`.`entity_id`) 
            AND (`at_name`.`attribute_id` = "'.$name_id.'") 
            AND (`at_name`.`store_id` = '.$store.')) AS `product_name_table`
        ON (`main_table`.`product_id` = `product_name_table`.`entity_id`)
LEFT JOIN (SELECT `e`.`entity_id`, `at_price`.`value` AS `price`  
        FROM `catalog_product_entity` AS `e` 
        INNER JOIN `catalog_product_entity_decimal` AS `at_price` 
                ON (`at_price`.`entity_id` = `e`.`entity_id`) 
                AND (`at_price`.`attribute_id` = "'.$price_id.'") 
                AND (`at_price`.`store_id` = '.$store.')) AS `product_price_table`
        ON (`main_table`.`product_id` = `product_price_table`.`entity_id`)

GROUP BY `main_table`.`answer_id`, `main_table`.`product_id` 
ORDER BY `answer_id` ASC'

如何在“加入”中进行嵌套选择?任何建议和帮助将不胜感激。

【问题讨论】:

    标签: mysql magento zend-db-select


    【解决方案1】:

    Zend_Db_Select 有一个 __toString() 方法,所以我很确定您可以为连接构造嵌套选择,然后在主查询中使用它。像这样的...

    // $adapter is your Zend_Db_Adapter...
    $joinConditionName = '`at_name`.`entity_id` = `e`.`entity_id`' 
        . $adapter->quoteInto('`at_name`.`attribute_id` = ?', $name_id)
        . $adapter->quoteInfo('`at_name`.`store_id` = ?', $store);
    $subQueryName = new Zend_Db_Select();
    $subQueryName->from(array('e' => 'catalog_product_entity'))
        ->join(
             'at_name' => 'catalog_product_entity', 
             $joinConditionName
        )->columns(array('e.entity_id', 'at_name.value'));
    
    
    // and then the main query
    $query = new Zend_Db_Select();
    $query->from('main_table' => 'nanorepwidgets_answer')
        ->joinLeft(array('product_name_table' => $subQueryName->__toString()),
            'main_table.product_id = produce_name_table.entity_id')
    

    我认为,或者类似的方法应该可行。它没有记录,但阅读源代码(1.12),您可以将 Zend_Db_Select 作为第一个参数传递给 joinLeft,然后您的子查询需要定义别名。我想,一切都是一样的。

    您还可以通过使用更简单的 ON 子句 (at_name.entity_id = e.entity_id) 然后使用 ->where('at_name.attribute_id = ?', $name_id) 使子选择中的引用更容易。这样就不再需要 $joinConditionName 位了。

    我很确定我已经做到了,虽然我现在找不到它,所以如果细节不完全正确,请道歉。

    【讨论】:

    • Zend_Db_Select 对象可以在任何你想要子查询的地方传递,无需转换为字符串。
    • 是的,我正在阅读 API 文档 (framework.zend.com/apidoc/1.10/… ),我认为它只允许 Zend_Db_Expr 或字符串,但阅读代码是对的。
    • 我用了同样的原理here(自我宣传提醒!)
    • 您的回答帮我解决了问题,谢谢!
    猜你喜欢
    • 2016-07-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    相关资源
    最近更新 更多