(mysql)通过meta_key和product_id获取woocommerce订单

时间:2014-04-17 13:55:29

标签: php mysql wordpress woocommerce

我有一个mysql问题。

我有这个选择

SELECT DISTINCT t2.meta_key,t2.meta_value
FROM wp_woocommerce_order_items AS t1
JOIN wp_woocommerce_order_itemmeta AS t2 ON t1.order_item_id = t2.order_item_id
JOIN wp_posts as t3 ON t3.ID=t1.order_id
WHERE t2.order_item_id IN(SELECT distinct t1.order_item_id FROM wp_woocommerce_order_items AS t1
JOIN wp_woocommerce_order_itemmeta AS t2 ON t1.order_item_id = t2.order_item_id WHERE t2.meta_key like "_product_id" AND t2.meta_value = 99) AND t2.meta_key="member_id" AND t3.post_status like "publish"

此选择查询获取所有“member_id”(meta_key),其中“_product_id”等于99.

我有一个好结果。它可能不是“优雅的”,但它是有效的。

我现在的问题是,我需要获取_product_id等于99 AND的所有member_id 订单未被取消。

另外,我不知道哪个表以及“order_status”的真正关键是什么。

所以,

谢谢你的帮助!

修改

我添加了

JOIN wp_posts as t3 ON t3.ID=t1.order_id

WHERE t3.post_status = "publish"

我需要的当前值在表中:wp_term。

感谢

1 个答案:

答案 0 :(得分:1)

好吧,我明白了。

此查询采用所有" meta_value" (这里:member_id)

  • 在所有订单上
    • 订单状态不是:"失败' '取消'或者'退款'
    • 其中订单的产品ID为99
    • 订单不在垃圾桶里。

所以:

SELECT t2.meta_value
FROM wp_woocommerce_order_items AS t1
JOIN wp_woocommerce_order_itemmeta AS t2 ON t1.order_item_id = t2.order_item_id
WHERE t2.order_item_id IN(SELECT DISTINCT oi.order_item_id
FROM wp_posts, wp_postmeta, wp_term_relationships tr,wp_term_taxonomy tt,
     wp_terms te, wp_woocommerce_order_itemmeta oi, wp_woocommerce_order_items ii 
WHERE tt.taxonomy like "shop_order_status"
AND te.term_id = tt.term_id
AND te.slug  NOT IN ('failed','cancelled','refunded')
AND tr.term_taxonomy_id = tt.term_taxonomy_id
AND oi.order_item_id = ii.order_item_id
AND oi.meta_key = '_product_id'
AND oi.meta_value = 99
AND ii.order_id = wp_posts.ID
AND wp_postmeta.post_id = tr.object_id
AND wp_posts.post_status = 'publish') AND t2.meta_key like "member_id"

我希望能帮助一些人。

如果您对此查询有更优雅或更优化。

随时告诉我们: - )