繁体   English   中英

SQL 查询从一个表求和到另一个表

[英]SQL query for summing from one table into another

我有两张桌子:

https://imgur.com/DQAXAeA

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”将如下所示:

https://imgur.com/7b8NTxN

提前致谢。

试试下面的 - 更新加入

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.

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