mysql-woo-commerce-产品、类别、价格和数量表

yhived7q  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(361)

我正在尝试为快速销售报告构建以下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

igetnqfo

igetnqfo1#

我能够让这个工作,我只需要创建子查询的类别查找,并计算每个产品的销售。
解决方案:

SELECT
    (SELECT wp_terms.name
        FROM wp_terms
        JOIN wp_term_relationships ON wp_term_relationships.term_taxonomy_id = wp_terms.term_id
        WHERE wp_term_relationships.term_taxonomy_id = wp_terms.term_id
        AND wp_terms.term_id > 23
        AND wp_term_relationships.object_id = wp_posts.ID) AS 'Category',

    wp_posts.post_title AS 'Product',

    (SELECT SUM(wp_woocommerce_order_itemmeta.meta_value)
        FROM wp_woocommerce_order_itemmeta
        JOIN wp_woocommerce_order_items ON wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
        WHERE wp_woocommerce_order_itemmeta.meta_key = '_qty'
        AND wp_woocommerce_order_items.order_item_name = wp_posts.post_title) AS 'Quantity',

    MAX(CASE WHEN wp_postmeta.meta_key = '_price' THEN wp_postmeta.meta_value ELSE NULL END) AS 'Price'

FROM wp_posts
    LEFT JOIN wp_postmeta ON wp_postmeta.post_id = wp_posts.ID
    LEFT JOIN wp_term_relationships ON wp_term_relationships.object_id = wp_posts.ID
    LEFT JOIN wp_term_taxonomy ON (wp_term_taxonomy.taxonomy = 'product_cat' AND wp_term_relationships.object_id = wp_term_taxonomy.term_taxonomy_id)
    LEFT JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id

WHERE
    wp_posts.post_type IN('product', 'product_variation') AND wp_posts.post_status = 'publish' AND wp_posts.post_content <> ''

GROUP BY
    Category, Product

相关问题