您好,我想知道如何编写php语句来执行一些sql查询。magento如何以最低价格列出最受欢迎的产品
例如:(列出所有产品以最小的代价)
$collection1 = Mage::getModel('catalog/product')->getCollection();
$collection1
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addAttributeToFilter('attribute_set_id', 4)
->setOrder('created_at', 'desc')
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents();
SQL查询是这样说的:
SELECT `e`.*, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`,
IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`,
`price_index`.`tier_price` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_product_index_price` AS
`price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND
price_index.customer_group_id = 0 WHERE (`e`.`attribute_set_id` = '4') ORDER BY `e`.`created_at` desc
但我知道下面的PHP语句可以列出观看次数最多的产品:
$collection2 = Mage::getResourceModel('reports/product_collection');
$collection2->addViewsCount();
然后,sql查询就是这样的:
SELECT COUNT(report_table_views.event_id) AS `views`, `e`.* FROM `report_event` AS `report_table_views`
INNER JOIN `catalog_product_entity` AS `e` ON e.entity_id = report_table_views.object_id AND e.entity_type_id = 4
WHERE (report_table_views.event_type_id = 1) GROUP BY `e`.`entity_id` HAVING
(COUNT(report_table_views.event_id) > 0) ORDER BY `views` DESC
我在想怎么写PHP语句结合上述两个SQL查询,我想说明从下面的SQL查询的结果。
SELECT COUNT(report_table_views.event_id) AS `views`, `e`.*, `price_index`.`price`, `price_index`.`tax_class_id`,
`price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price,
price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`,
`price_index`.`max_price`, `price_index`.`tier_price` FROM `report_event` AS `report_table_views` INNER JOIN
`catalog_product_entity` AS `e` ON e.entity_id = report_table_views.object_id AND e.entity_type_id = 4 INNER JOIN
`catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id =
'1' AND price_index.customer_group_id = 0 WHERE (report_table_views.event_type_id = 1 AND `e`.`attribute_set_id` =
'4') GROUP BY `e`.`entity_id` HAVING (COUNT(report_table_views.event_id) > 0) ORDER BY `views` DESC
我以为我可以写下面的PHP语句,但它不工作,也许addViewsCount()是覆盖整个SQL语句。
$collection3 = Mage::getResourceModel('reports/product_collection');
$collection3
->addAttributeToSelect('*')
->addAttributeToFilter('attribute_set_id', 4)
->addOrderedQty()
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addViewsCount();
现在,谁又能知道怎么写PHP语句,以最小的价格上市观看次数最多的产品,请原谅我,我有一个属性集ID必须为4对所有产品实体麻烦的要求。我知道这个问题是非常具有挑战性的,因为我不知道从谷歌搜索这些信息。请大家看看问题。感谢每一个努力。
我认为这些PHP语句正在工作。 \t $ storeId = Mage :: app() - > getStore() - > getId(); \t $ collection3 = Mage :: getResourceModel('reports/product_collection') - > setStoreId($ storeId); \t $ collection3-> addViewsCount(); \t $ collection3-> addAttributeToFilter('attribute_set_id',4); \t $ collection3-> addAttributeToSelect('*'); \t $ collection3-> addStoreFilter($ storeId); \t $ collection3-> addMinimalPrice(); \t $ collection3-> addFinalPrice(); \t $ collection3-> addTaxPercents(); – Jun