October 22, 2024
Chicago 12, Melborne City, USA
SQL

How can I do complex calculations in MYSQL?


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?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video