Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7997861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:04:11+00:00 2026-06-04T15:04:11+00:00

I have 2 tables: stock: pid name qty — —- — 1 aaaa 2

  • 0

I have 2 tables:

stock:

pid name qty
--- ---- ---
1   aaaa   2
2   bbbb   3
1   aaaa   5
3   cccc   1
2   bbbb   2

stock_total:

pid name total_qty
--- ---- ---------

I can insert rows from stock table with the total qty to stock_total using this query

INSERT INTO stock_total (pid, name, total_qty)
SELECT pid, name, SUM(qty) 
FROM stock
GROUP BY pid, name

The problem is, I will run the SQL above via cron job. So on the next execution, the SQL should UPDATE existing product and INSERT non-exist products.

It would be very inefficient if I loop over the SELECT results, check each row if exists in stock_total and do the INSERT or UPDATE.

Is there any simpler way for achieving this? perhaps by modifying the SQL above. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:04:12+00:00Added an answer on June 4, 2026 at 3:04 pm

    After some googling and experimenting with the answers here, I came up with this solution. It turns out that MySQL supports REPLACE INTO... and ...ON DUPLICATE KEY UPDATE. So my query would be like this:

    REPLACE INTO stock_total
    SELECT pid, name, SUM(qty)
    FROM stock
    GROUP by pid, name
    

    or,

    INSERT INTO stock_total
    SELECT pid, name, SUM(qty)
    FROM stock
    GROUP by pid, name
    ON DUPLICATE KEY UPDATE total_qty=VALUES(total_qty)
    

    If a row exists on stock_total, the first query will DELETE then INSERT the new row,
    and the second query will UPDATE the existing row.

    Both query will only work if the table has a primary key or unique index:

    CREATE TABLE stock_total (
        pid         INT         NOT NULL,
        name        VARCHAR(20) NOT NULL,
        total_qty   INT         NOT NULL,
    
        UNIQUE (pid, name)
    );
    

    Documentation:

    • REPLACE syntax
    • INSERT … ON DUPLICATE KEY UPDATE syntax
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tables like this: Stock Table product_id bigint(20) qty float Sales Table
I have three tables that hold items like: Products: ID NAME CATEGORY STOCK 1
I have a stock transaction table like this: StockID Item TransDate TranType BatchNo Qty
I have these tables: Stock, Unit, Location, Category, StockBalance At StockBalance: there is StockID
Suppose I have two tables: Table A has information about the stock holdings of
I have two tables, a vehicle table with columns: id stock year make model
I have two tables, splits and dividends that describe events in the stock market.
Let say that I have 2 tables with the same structure : STOCK and
I am using MySQL and I have two tables: master_table ORDERNO ITEM QTY stock_bal
I have 2 tables Products and ProductDetails. Products contains all information about my stock

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.