WooCommerce-查询订购产品

时间:2016-01-15 作者:JackTheKnife

我正在尝试获取特定日期/时间的所有订单,然后使用SQL(MySQL)订购产品,如下查询所示:

select 
    p.ID as order_id,
    p.post_date,
    i.order_item_name,
    max( CASE WHEN im.meta_key = \'_product_id\' and p.ID = im.order_item_id THEN im.meta_value END ) as Prod_ID
from 
    wp_posts as p,
    wp_postmeta as pm,
    wp_woocommerce_order_items as i,
    wp_woocommerce_order_itemmeta as im
where 
    p.post_type = \'shop_order\'
    and p.ID = pm.post_id
    and p.ID = i.order_id
    and p.post_date BETWEEN \'2016-01-14 00:00:00\' AND \'2016-01-14 23:59:59\'
    and p.post_status = \'wc-processing\'
当我尝试查询时wp_woocommerce_order_itemmeta 订购产品数据表我正在断开与数据库的连接(查询期间连接超时)。

有什么线索吗?

2 个回复
最合适的回答,由SO网友:JackTheKnife 整理而成

固定查询

select 
        p.ID as order_id,
        p.post_date,
        i.order_item_name,
        max( CASE WHEN im.meta_key = \'_product_id\' and i.order_item_id = im.order_item_id THEN im.meta_value END ) as Prod_ID
    from 
        wp_posts as p,
        wp_postmeta as pm,
        wp_woocommerce_order_items as i,
        wp_woocommerce_order_itemmeta as im
    where 
        p.post_type = \'shop_order\'
        and p.ID = pm.post_id
        and p.ID = i.order_id
        and p.post_date BETWEEN \'2016-01-14 00:00:00\' AND \'2016-01-14 23:59:59\'
        and p.post_status = \'wc-processing\'
说明:Post ID与订单项元没有直接关系

SO网友:user2711750

我在构建自己的报告时发现,我想更新这个。

更好的方法是进行适当的内部联接,以最大限度地减少查询时间:

select 
    p.ID as order_id,
    p.post_date,
    i.order_item_name,
    max( CASE WHEN im.meta_key = \'_product_id\' and i.order_item_id = im.order_item_id THEN im.meta_value END ) as Prod_ID
from 
    wp_posts as p
    inner join
    wp_postmeta as pm 
    on p.id = pm.post_id
    inner join
    wp_woocommerce_order_items as i 
    on p.id = i.order_id
    inner join 
    wp_woocommerce_order_itemmeta as im 
    on i.order_item_id = im.order_item_id
where 
    p.post_type = \'shop_order\'
    and p.post_date BETWEEN \'2017-04-29 00:00:00\' AND \'2017-04-30 23:59:59\'
    and p.post_status = \'wc-processing\';

相关推荐

将wp_Query替换为wp_User_Query

我在插件中制作了一些订阅者档案和单曲(个人资料页),其中我还包括了一个“用户单曲”模板,通过template_include. 不过,我正在尝试从插件中删除一些模板,以使其使用主题模板。我用了locate_template( \'single.php\' ) 从活动主题中选择单个模板。我没有使用全局wp_query 在本例中显示我的内容,但页面显示了基于查询默认值的循环(十篇帖子)。我想知道的是,我是否可以完全放弃默认查询,用wp_user_query 我可以将查询到的用户ID输入其中。然后我想筛选the