【问题标题】:Get counter of product units of several orders获取多个订单产品单位的计数器
【发布时间】:2020-03-20 16:06:50
【问题描述】:

经过大量尝试,我得到了一个查询,该查询返回了一天中所有订单的所有产品和每个产品的单位。

这是:

SELECT
    it.order_id as pedido,
    it.order_item_name as nombre,
    it.order_item_id,
    MAX(CASE WHEN itm.meta_key = '_product_id' THEN itm.meta_value END) as producto,
    MAX(CASE WHEN itm.meta_key = '_qty' THEN itm.meta_value END) as qt,
    (SELECT meta_value FROM wp_postmeta where meta_key = '_weight' and post_id = 
    MAX(CASE WHEN itm.meta_key = '_product_id' THEN itm.meta_value END)
    ) as peso,
    (SELECT meta_value FROM wp_postmeta where meta_key = 'entrega' and post_id = 
    p.ID
    ) as fecha
FROM wp_posts as p
    inner join wp_woocommerce_order_items as it
        on it.order_id = p.ID
    inner join wp_woocommerce_order_itemmeta as itm
        on itm.order_item_id = it.order_item_id
    inner join wp_postmeta as pm
        on pm.post_id = p.ID
WHERE order_item_type = 'line_item' 
    and it.order_item_id = itm.order_item_id
    and itm.meta_key = '_qty' or itm.meta_key = '_product_id'
GROUP BY order_item_id

它工作得很好,但现在我希望它只适用于状态为:“处理中”的订单

我在Where中添加了这个:

and p.post_status = 'wc-processing'

什么也没发生。

谁能帮帮我?

【问题讨论】:

    标签: mysql wordpress woocommerce


    【解决方案1】:

    你应该避免混合 ands 和 ors,使用括号来强制你想要的条件的优先级-

    WHERE order_item_type = 'line_item' 
        and it.order_item_id = itm.order_item_id
        and (itm.meta_key = '_qty' or itm.meta_key = '_product_id')
        and p.post_status = 'wc-processing'
    

    【讨论】:

    • 真的很简单。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    相关资源
    最近更新 更多