mySQL WooCommerce-产品表,类别,价格和数量

时间:2018-11-06 13:27:24

标签: mysql sql wordpress woocommerce

我正在尝试为快速销售报告构建以下mySQL表:

result.Status = args[1]
licenceAsBytes, _ = json.Marshal(result)

但是我现在在:

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

亲切的问候, M

1 个答案:

答案 0 :(得分:0)

我能够使它正常工作,我只需要为类别查找创建子查询,并计算所售出的每种产品。

解决方案:

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