How can I find all rows in a table of data, which are relative to the result of some calculation between data from rows in another table?
(As an example, for an eshop, I want to find all products (rows) in a table of products, which are having a price (column in another table) that is below the sum of the relative shipping price, plus the regular price of this product (that data are in another table).
How can I achieve this?
To make it more clear, lets try it with a woocommerce MYSQL query:
SELECT * FROM eu_posts p WHERE p.ID IN (SELECT post_id from eu_postmeta m where m.meta_key="_regular_price" and m.meta_value < (SELECT m.meta_value WHERE m.meta_key='shipping_cost' and m.post_id = p.ID) + (Select m.meta_value WHERE m.meta_key='bb_price' and m.post_id = p.ID));
but the above returns an empty set, though there are rows of products with price lower than the expected sum of the two other values.
Is it possible to do such calculations inside MYSQL?
2
Answers
It seems I found an answer, the following query works:
The key seems to be, as Barmar seems to suggest, to name values differently.
You should join with
wp_postmeta
multiple times, not use correlated subqueries.