2
我想修改以下MySQL查詢Magento。它現在所做的是返回產品信息,以獲取報告的簡單和可配置產品。 不幸的是,簡單的產品並不具有父配置產品所具有的所有信息。Magento sql查詢幫助需要與海量查詢
我想要做的是使用表catalog_product_super_link
查找每個產品的父產品ID,以便對於所有產品,我都會獲取父配置產品的數據。超鏈接表在一列中包含所有產品ID的值,在另一列中包含相應的父代。對於可配置產品,產品ID和父ID是相同的。
有意義嗎?
我有查詢是:
SELECT IFNULL(category_name.value, category_default_name.value) AS `primarycategory`
, `product`.`sku`
, IFNULL(product_name.value, product_default_name.value) AS `product_name`
, `attribute_option_value`.`value` AS `story`
, (
select GROUP_CONCAT(category_name.value)
from catalog_category_entity as category
, catalog_category_entity_varchar as category_name
, catalog_category_product as category_product
WHERE category_name.entity_id = category.entity_id
AND category_name.store_id = 0
AND category_name.entity_type_id = 3
AND category_name.attribute_id = 33
AND category_product.product_id = order_item.product_id
AND category_product.category_id = category.entity_id
AND category_name.value<>'Default Category'
) AS `category`
, round(sum(order_item.qty_ordered)) AS `unitssoldnum`
, ROUND(sum(order_item.price-order_item.discount_amount-order_item.tax_amount)
, 2
) AS `unitssoldcur`
, ROUND((
SUM(order_item.price-order_item.discount_amount-order_item.tax_amount)
- (IFNULL(product_cost.value, product_default_cost.value) * SUM(order_item.qty_ordered))
)
, 2
) AS `grossmargincur`
, ROUND((
(
SUM(order_item.price-order_item.discount_amount-order_item.tax_amount)
- (IFNULL(product_cost.value, product_default_cost.value) * SUM(order_item.qty_ordered))
)
/(SUM(order_item.price-order_item.discount_amount-order_item.tax_amount))
)
* 100
, 2
) AS `grossmarginpercent`
, (
SELECT round(SUM(qty))
FROM cataloginventory_stock_item AS stock
, catalog_product_super_link AS product_link
WHERE product_link.product_id = stock.product_id
AND product_link.parent_id=order_item.product_id
) AS `sohatstore`
, (
SELECT round(SUM(qty)/SUM(order_item.qty_ordered))
FROM cataloginventory_stock_item AS stock
, catalog_product_super_link AS product_link
WHERE product_link.product_id = stock.product_id
AND product_link.parent_id=order_item.product_id
) AS `storecover`
, (
SELECT round(SUM(order_item.qty_ordered)
/(SUM(qty)+SUM(order_item.qty_ordered))
*100
, 2
)
FROM cataloginventory_stock_item AS stock
, catalog_product_super_link AS product_link
WHERE product_link.product_id = stock.product_id
AND product_link.parent_id=order_item.product_id
) AS `sellthrupercent`
, ROUND(SUM(order_item.price),2) AS `originalretail`
, ROUND(SUM(IFNULL(product_cost.value, product_default_cost.value))
, 2
) AS `costextax`
, (
SELECT round(SUM(qty_ordered))
FROM sales_flat_order_item AS order_total
WHERE order_total.product_id = order_item.product_id
) AS `totalsoldtilldate`
, (
SELECT ROUND((
SELECT round(SUM(qty_ordered))
FROM sales_flat_order_item AS order_total
WHERE order_total.product_id = order_item.product_id
)
/(
(
SELECT round(SUM(qty_ordered))
FROM sales_flat_order_item AS order_total
WHERE order_total.product_id = order_item.product_id
)
+(
SELECT round(SUM(qty))
FROM cataloginventory_stock_item AS stock
, catalog_product_super_link AS product_link
WHERE product_link.product_id = stock.product_id
AND product_link.parent_id=order_item.product_id
)
)*100
, 2
)
) AS `totalsellthru`
, (
SELECT ROUND((
(
sum(order_total.price-order_total.discount_amount-order_total.tax_amount)
- sum(order_total.base_cost * order_total.qty_ordered)
)
/sum(order_total.price-order_total.discount_amount-order_total.tax_amount)
)*100
, 2
)
FROM sales_flat_order_item AS order_total
WHERE order_total.product_id = order_item.product_id
) AS `totalgrossmarginpercent`
FROM `sales_flat_order` AS `order`
INNER JOIN `sales_flat_order_item` AS `order_item` ON order_item.order_id = order.entity_id
INNER JOIN `catalog_product_entity` AS `product`
ON product.entity_id = order_item.product_id
AND product.entity_type_id = 4
AND product.type_id NOT IN('grouped', 'bundle')
LEFT JOIN `catalog_product_entity_varchar` AS `product_name`
ON product_name.entity_id = product.entity_id
AND product_name.store_id = order.store_id
AND product_name.entity_type_id = 4
AND product_name.attribute_id = 60
LEFT JOIN `catalog_product_entity_varchar` AS `product_default_name`
ON product_default_name.entity_id = product.entity_id
AND product_default_name.store_id = 0
AND product_default_name.entity_type_id = 4
AND product_default_name.attribute_id = 60
LEFT JOIN `catalog_product_entity_decimal` AS `product_cost`
ON product_cost.entity_id = product.entity_id
AND product_cost.store_id = order.store_id
AND product_cost.entity_type_id = 4
AND product_cost.attribute_id = 68
LEFT JOIN `catalog_product_entity_decimal` AS `product_default_cost`
ON product_default_cost.entity_id = product.entity_id
AND product_default_cost.store_id = 0
AND product_default_cost.entity_type_id = 4
AND product_default_cost.attribute_id = 68
LEFT JOIN `catalog_category_entity` AS `category`
ON category.entity_id = (select MAX(category_id) from catalog_category_product where product_id = product.entity_id)
AND category.entity_type_id = 3
LEFT JOIN `catalog_category_entity_varchar` AS `category_name`
ON category_name.entity_id = category.parent_id
AND category_name.store_id = order.store_id
AND category_name.entity_type_id = 3
AND category_name.attribute_id = 33
LEFT JOIN `catalog_category_entity_varchar` AS `category_default_name`
ON category_default_name.entity_id = category.parent_id
AND category_default_name.store_id = 0
AND category_default_name.entity_type_id = 3
AND category_default_name.attribute_id = 33
LEFT JOIN `catalog_product_entity_int` AS `story_name`
ON story_name.entity_id = product.entity_id
AND story_name.store_id = 1
AND story_name.entity_type_id = 4
AND story_name.attribute_id = 138
LEFT JOIN `catalog_product_entity_int` AS `story_default_name`
ON story_default_name.entity_id = product.entity_id
AND story_default_name.store_id = 0
AND story_default_name.entity_type_id = 4
AND story_default_name.attribute_id = 138
LEFT JOIN `eav_attribute_option` AS `attribute_option`
ON attribute_option.option_id = IFNULL(story_name.value, story_default_name.value)
LEFT JOIN `eav_attribute_option_value` AS `attribute_option_value`
ON attribute_option_value.option_id = attribute_option.option_id
AND attribute_option_value.store_id = 0
WHERE (order.state <> 'canceled')
AND (DATE(order.created_at) <= DATE('2012-07-03'))
AND (DATE(order.created_at) >= DATE('2012-07-01'))
GROUP BY `product_name`
ORDER BY `category_name`.`value` ASC
, `category_default_name`.`value` ASC
, `product_name`.`value` ASC
, `product_default_name`.`value` ASC
如果任何人都可以在正確的方向點我將不勝感激:)
下面是輸出的一個示例中,我得到了配置的產品:
"Category Name",Product,Name,Story,"Category Description","Units Sold (#)","Units Sold ($)","GM ($)","GM (%)","SOH at Store","Store Cover","Sell through %","Original Retail Price","Cost Ex Tax","Total Units Sold (#)","Total Sell Thru %","GM% Total"
Bottoms,31875,"Denim Babe Jean",DENIM,"Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$59.84,$39.25,65.59,118,59,1.67,$68.00,$10.30,81,40.70,69.38
Bottoms,34475,"Diamante Rip Boot Leg Jean",DENIM,"Bottoms,SALE,Denim,Jeans,Bootleg Jean,Pants",2,$29.92,-$7.56,-25.25,28,14,6.67,$34.00,$18.74,5,15.15,19.90
Bottoms,35853,"Embroidered Back Wide Leg Jean",DENIM,"Bottoms,SALE,Denim,Jeans,Wideleg Jean,Pants",2,$29.92,-$9.08,-30.35,11,6,15.38,$34.00,$19.50,22,66.67,23.93
Bottoms,34278,"Firecracker Bootleg Jean",DENIM,"Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$59.84,$29.92,50.00,9,5,18.18,$68.00,$14.96,39,81.25,43.99
Bottoms,28450,"Glamourzon Jean","CHIC","Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$59.84,$59.84,100.00,101,51,1.94,$68.00,$0.00,51,33.55,100.00
這裏是我用簡單的產品得到的樣品。注意所有缺失的數據。這就是爲什麼我的報告要查找父產品ID並使用它的數據。
"Category Name",Product,Name,Story,"Category Description","Units Sold (#)","Units Sold ($)","GM ($)","GM (%)","SOH at Store","Store Cover","Sell through %","Original Retail Price","Cost Ex Tax","Total Units Sold (#)","Total Sell Thru %","GM% Total"
Bottoms,2000004614430,"Denim Babe Jean - Dark Denim - 24",DENIM,"Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$0.00,-$20.59,,,,,$0.00,$10.30,15,,
Bottoms,2000004901523,"Diamante Rip Boot Leg Jean - Black - 14",DENIM,"Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$0.00,-$37.48,,,,,$0.00,$18.74,2,,
Bottoms,2000004883881,"Firecracker Bootleg Jean - Denim Dark - 16",DENIM,"Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$0.00,-$29.92,,,,,$0.00,$14.96,9,,
Bottoms,2000004159955,"Glamourzon Jean - Dark Denim - 24","CHIC","Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$0.00,$0.00,,,,,$0.00,$0.00,11,,
Bottoms,2005010849432,"Glamourzon Night Jean - Black - 28","CHIC","Bottoms,Denim,Jeans,Bootleg Jean,Pants",2,$0.00,-$32.80,,,,,$0.00,$16.40,7,,-68.82
我希望能回答你的問題。謝謝!
您可以給出您想要的較低級別條目的示例轉儲,並使用相應的父級,從而在較低級別顯示您想要的其他條目。我想我明白你在找什麼,但不想強迫我先解釋。 - 每張表4-5個記錄以獲得樣本理解。 – DRapp 2012-07-15 21:51:58