Skip to content

Instantly share code, notes, and snippets.

@rfmeier
Created November 7, 2017 03:29
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 rfmeier/3e31923ff974669456fccf2ac6507a9b to your computer and use it in GitHub Desktop.
Save rfmeier/3e31923ff974669456fccf2ac6507a9b to your computer and use it in GitHub Desktop.
Select all orders for a product with MySql
SELECT
`items`.`order_id`,
MAX(CASE WHEN `itemmeta`.`meta_key` = '_product_id' THEN `itemmeta`.`meta_value` END) AS `product_id`
FROM
`wp_woocommerce_order_items` AS `items`
INNER JOIN
`wp_woocommerce_order_itemmeta` AS `itemmeta`
ON
`items`.`order_item_id` = `itemmeta`.`order_item_id`
WHERE
`items`.`order_item_type` IN('line_item')
AND
`itemmeta`.`meta_key` IN('_product_id')
GROUP BY
`items`.`order_item_id`
HAVING
`product_id` = 8 # Product ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment