[英]SQL query for summing from one table into another
I have two tables:我有两张桌子:
What I needs is SQL query that I can run via the SQL tab in phpMyAdmin, which will sum the "options_stock" from "table_options" based on the "product_id", and it will write that sum number into "main_stock" in "table_products" for the corresponding "product_id", so the "table_products" will look like this: What I needs is SQL query that I can run via the SQL tab in phpMyAdmin, which will sum the "options_stock" from "table_options" based on the "product_id", and it will write that sum number into "main_stock" in "table_products"对于相应的“product_id”,因此“table_products”将如下所示:
Thanks in advance.提前致谢。
Try the below - with update join试试下面的 - 更新加入
update table_product p
join
(select product_id, sum(options_stock) as options_stock from
table_options group by product_id
) o on p.product_id=o.product_id
set main_stock=options_stock
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.