I have a table (woocommerce) which has the following structure
I want to get the meta_value
corresponding to certain meta_key
and if they don’t exist then return NULL. This is what i have right now
SELECT `meta_value`
FROM `wp_woocommerce_order_itemmeta`
WHERE `order_item_id` = 66
AND `meta_key` IN ("pa_brewing-method", "pa_size", "Sold By")
Result :
meta_value
BeanDeck
How can i show this as?
meta_value
NULL
NULL
BeanDeck
2
Answers
You aren’t correlating the inner and outer queries. If there’s at least one product with category_id = 90 the inner query will return some rows, and thus the NOT EXISTS condition will always be false, and the outer query will return no rows. You need to add a condition to specify the inner query and the outer query refer to the same product:
Instead of filtering using
IN()
, you couldLEFT JOIN
on to the list of keys you’re interested in.