Created
November 7, 2017 03:29
-
-
Save rfmeier/3e31923ff974669456fccf2ac6507a9b to your computer and use it in GitHub Desktop.
Select all orders for a product with MySql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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