Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save connorhu/61295c8d02187d678ae5bb59838800a2 to your computer and use it in GitHub Desktop.
Save connorhu/61295c8d02187d678ae5bb59838800a2 to your computer and use it in GitHub Desktop.
WP Gists
SET @prefix = 'wp_';
SET @date_from = '2020-01-01';
SET @date_to = '2020-12-31';
SET @query = CONCAT('SELECT sum(woim.meta_value) number_of_item_sold, woi.order_item_name
FROM ', @prefix, 'woocommerce_order_itemmeta as woim
LEFT JOIN ', @prefix, 'woocommerce_order_items as woi ON woi.order_item_id = woim.order_item_id
LEFT JOIN ', @prefix, 'posts as p ON woi.order_id = p.ID
WHERE woim.meta_key = \'_qty\'
AND p.post_status = \'wc-completed\'
AND p.post_date BETWEEN \'', @date_from, '\' AND \'', @date_to, '\'
GROUP BY woi.order_item_name
ORDER BY number_of_item_sold DESC');
PREPARE stmt FROM @query;
EXECUTE stmt;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment