我正在尝试为快速销售报告构建以下mysql表:
ID Category Product Category_ID Price Quantity
101 CAT-A ITEM-A 1001 5 3
102 CAT-C ITEM-F 1003 12 2
103 CAT-B ITEM-S 1002 15 6
然而,我目前在:
ID Category Product Category_ID Price Quantity
101 NULL ITEM-A NULL 5 NULL
102 NULL ITEM-F NULL 12 NULL
103 NULL ITEM-S NULL 15 NULL
我的查询看起来还可以,我已经把所有的部分单独地分解了,但是我认为我没有对数量和类别使用正确的函数。
SELECT
p.ID,
t.name AS 'Category',
p.post_title AS 'Product',
tt.term_taxonomy_id AS 'Category_ID',
MAX(CASE WHEN pm1.meta_key = '_price' THEN pm1.meta_value ELSE NULL END) as 'Price',
SUM(CASE WHEN oim.meta_key = '_qty' THEN oim.meta_value ELSE NULL END) as 'Quantity'
FROM wpdc_posts AS p
LEFT JOIN wpdc_postmeta AS pm1 ON pm1.post_id = p.ID
LEFT JOIN wpdc_term_relationships AS tr ON tr.object_id = p.ID
LEFT JOIN wpdc_term_taxonomy AS tt ON tt.taxonomy = 'product_cat' AND tr.object_id = tt.term_taxonomy_id
LEFT JOIN wpdc_terms AS t ON t.term_id = tt.term_id
LEFT JOIN wpdc_woocommerce_order_items AS oi ON oi.order_item_id = p.ID
LEFT JOIN wpdc_woocommerce_order_itemmeta AS oim ON oi.order_item_id = oim.order_item_id AND oim.meta_key = '_qty'
WHERE
p.post_type IN('product', 'product_variation') AND p.post_status = 'publish' AND p.post_content <> ''
GROUP BY
t.name,
p.post_title
如果我把这部分拿出来,分类会显示出来,但是项目太多了: AND tr.object_id = tt.term_taxonomy_id
谨致问候,m
1条答案
按热度按时间igetnqfo1#
我能够让这个工作,我只需要创建子查询的类别查找,并计算每个产品的销售。
解决方案: