如何在MYSQL中计算WordPress order_itemmeta的产品_qty的产品SUM()

我有这么复杂的MySQL查询:

SELECT im.order_item_id, im.meta_key, im.meta_value
FROM `wp_woocommerce_order_itemmeta` AS im
JOIN `wp_woocommerce_order_items` AS oi
ON oi.order_item_id = im.order_item_id
JOIN `wp_posts` AS p
ON p.ID = oi.order_id
WHERE oi.order_id IN (
    SELECT DISTINCT order_id
    FROM wp_tss_orders_history
    WHERE created BETWEEN 1477880000 AND 1689198400
)
AND im.meta_key IN ("_product_id", "_qty")
AND p.post_status <> "trash"
AND oi.order_item_type = "line_item"
ORDER BY im.order_item_id ASC

返回以下结果:

有什么办法可以修改我的查询(GROUP BY + SUM())以获得以下格式的结果:

array(
  array('_product_id' => 178, '_qty' => 150),
  array('_product_id' => 177, '_qty' => 20),
  array('_product_id' => some other ID, '_qty' => Number Of Items Sold),
)
评论