PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 12, 2022

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

 January 12, 2022     join, mysql, phpmyadmin, sql-update     No comments   

Issue

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:

https://imgur.com/7b8NTxN

Thanks in advance.


Solution

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 


Answered By - Fahmi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing