2014-02-17 82 views
2

我最近剛搬到WooCommerce平臺,並需要建立SQL在MySQL運行我自己的分析報告。例如,我需要報告顯示過去30天每個國家/地區的每個SKU的總銷售數量。 我探索了像'wp_posts','wp_postmeta'這樣的表格,並發現產品和訂單中的大部分字段都是基於記錄的,而不是基於列的,這使得SQL更具挑戰性。創建WooCommerce分析報告與SQL

+0

不知道我跟着:-( – Strawberry

+0

我想運行在MySQL工作臺報告顯示,上個月與在該產品被運到這個國家一起銷售的每個產品的數量。源表是WordPress的/ WooCommerce。 –

+0

答案是問題,所以它不再是一個問題。 – JasonMArcher

回答

3

最後我自己構建了SQL。希望對於不想使用現成分析解決方案或需要自定義報告的其他WooCommerce用戶有用。

SELECT 
    sku, count(1) 
FROM 
    wp_woocommerce_order_items oi, -- orders table 
    wp_posts p, -- woocommerce use posts to keep metadata on the order 
    wp_postmeta pm, -- we use this table to filter the completed orders 
    wp_postmeta pm1, -- we use this table again for getting the shipping country info 
    wp_woocommerce_order_itemmeta oim, -- use this table to get product ids from orders 
    (SELECT p.id as product_id, meta_value as sku FROM wp_posts p, wp_postmeta pm where 
     post_type='product' and post_status='publish' 
     and meta_key='_sku' 
     and p.id=pm.post_id) as sku_table -- building temp sku table from published products that defines relationship between product id and sku 
WHERE 
    oim.meta_value=sku_table.product_id AND 
    oi.order_id=p.ID AND 
    p.id=pm.post_id AND 
    pm.meta_key='_paid_date' AND -- here we make sure that the order was completed 
    p.id=pm1.post_id AND 
    order_item_type='line_item' AND -- get only line items but not tax or shipping 
    oi.order_item_id=oim.order_item_id AND 
    oim.meta_key='_product_id' AND 
    post_date>'2014-01-01' AND -- limits the report date 
    pm1.meta_key='_shipping_country' AND -- get shipping country 
    pm1.meta_value='US' -- limits the country 
GROUP BY sku 
ORDER BY sku;