簡體   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