简体   繁体   English

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

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

I have two tables:我有两张桌子:

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

https://imgur.com/7b8NTxN

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.

相关问题 SQL查询从一个表中选择不在另一表中的行 - SQL query to select row from one table which is not in another table SQL查询-从一个表中选择全部,在另一个表中匹配记录 - SQL Query - select all from one table with matching records in another 将数据从一个表复制到另一个表的 sql 查询中的语法错误 - Syntax error in sql query of copying data from one table to another 使用SQL查询将值从一个表插入另一个表 - INSERT values from one table to another using SQL Query 用于求和另一个表中值的 MySQL 查询 - MySQL query for summing values in another table SQL查询以从一个表中选择一个值,从另一表中选择另一个值,并显示计数 - SQL query to select one value form one table and other from another table and also display count 用于查询一个或另一个表的SQL查询 - SQL query for joining one or another table sql select查询一个表中的一行,而另一表中的多行 - sql select query for one row in one table and multiple row from another table SQL查询从一个表中选择,其中不在另一个表中或在该表中具有特定值 - SQL query to select from one table where either not in another table OR in that table with a specific value SQL查询从一个表获取所有记录,除特定记录,按日期,从另一个表 - SQL Query To Get All Records From One Table, Except A Specific Record, By Date, From Another Table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM