简体   繁体   English

减去两个不同表中的两个字段以将不同字段插入第二个表中的第三个字段

[英]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. 我们有一个数据库,该数据库由第三方从多维数据集购物车转换为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. 这是我第一次尝试sql / php,所以请多多包涵,不要笑太多。

  1. Get a product id # from the product_id column in Table1 从表1的product_id列中获取产品ID#
  2. Get the base price amount from price column in Table1 从表1的价格列中获取基本价格金额
  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 查找表2中列出的产品ID的每个实例(每种产品最多可以享受3层折扣),然后执行下一步
  4. Get the discount price from the discount column in Table2 for each instance of that product. 从表2的折扣列中获得该产品的每个实例的折扣价。
  5. Subtract price (table1) from discount (table2) to find the reduction amount. 从折扣(表2)中减去价格(表1)以找到减少量。
  6. Insert reduction amount into the reduction column in Table2. 将减少量插入表2的减少列中。
  7. Repeat this for every row in Table2 对表2中的每一行重复此操作

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? 因此,如果我正确理解表1的产品ID,基本价格以及表2的折扣?

It looks like you want to take table 2 and just add a column that has price after discount on every row? 看来您想使用表2,仅添加一列,每行都有折后价格?

Let me know and I can try and guide you further.... 让我知道,我可以尝试进一步指导您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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