简体   繁体   中英

Subtract two fields in two different tables to insert different into a third field in second table

We have a db that was converted by a third party from cube cart to prestashop. Mostly things went fine but recently found a couple thousand products that lack a discount reduction value.

I have found bits and pieces on how to do what we need but I just need help. This is my first foray into sql/php so bear with me and dont laugh too much.

  1. Get a product id # from the product_id column in Table1
  2. Get the base price amount from price column in Table1
  3. Find every instance of the product id listed in Table2 (can be up to 3 tiers of discount for each product)and do the next steps
  4. Get the discount price from the discount column in Table2 for each instance of that product.
  5. Subtract price (table1) from discount (table2) to find the reduction amount.
  6. Insert reduction amount into the reduction column in Table2.
  7. Repeat this for every row in Table2

After research I learned enough to do it within the same table but this playing with values from two different tables my brain goes "Im outta here."

SELECT *, (price - discount) AS Sum FROM Table1

I found some examples but nothing crosses over close enough to my needs to work or my syntax is messing things up.

Even a nudge in the right direction would mean a lot.

select b.*, (b.price - a.discount) as sum
from table2 b
left join table1 a on b.product_id = a.product_id

So if I understand this correctly table1 has product Id, base price, and table2 has discount?

It looks like you want to take table 2 and just add a column that has price after discount on every row?

Let me know and I can try and guide you further....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM