[英]convert Raw SQL to Zend_Db_Select
I need to convert this sql query to Zend_Db_Select object 我需要将此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'
How can I do the nested selects in the "join" ? 如何在“联接”中进行嵌套选择? any suggestion and help will be greatly appreciated. 任何建议和帮助将不胜感激。
Zend_Db_Select has a __toString() method, so I'm pretty sure you can construct the nested select for the join, and then use it in the main query. Zend_Db_Select具有__toString()方法,因此,我很确定您可以为联接构造嵌套的select,然后在主查询中使用它。 Something like this... 像这样
// $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')
Or something along those lines should work, I think. 我认为,还是应该遵循这些方针。 It's not documented but reading the source (1.12) you can pass a Zend_Db_Select as the first argument to joinLeft, and your subquery would then need to define the alias. 它没有记录,但是在阅读源代码(1.12)时,您可以将Zend_Db_Select作为joinLeft的第一个参数传递,然后您的子查询将需要定义别名。 All comes to the same thing, I think. 我认为,所有事情都是一样的。
You could also make the quoting easier in the sub-select by using a simpler ON clause (at_name.entity_id = e.entity_id) and then using ->where('at_name.attribute_id = ?', $name_id). 通过使用更简单的ON子句(at_name.entity_id = e.entity_id),然后使用-> where('at_name.attribute_id =?',$ name_id),也可以使子选择中的引用更容易。 That'd remove the need for the $joinConditionName bit. 这样就不需要$ joinConditionName位。
I'm pretty sure I've done this, although tbh I can't find it right now, so apologies if the detail isn't quite right. 我很确定我已经做到了,尽管我现在无法找到它,所以抱歉,如果细节不够正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.