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

  • SEARCH
  • Home
  • 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 356379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:06:22+00:00 2026-05-12T12:06:22+00:00

I have 2 tables like this: Stock Table product_id bigint(20) qty float Sales Table

  • 0

I have 2 tables like this:

Stock Table
product_id bigint(20)
qty float

Sales Table
product_id bigint(20)
qty float

Sample Data
Stock Table
product_id—qty
1—10
2—11
3—20
4—50
1—10
3—10

Sales Table
product_id—qty
1—2
2—5
3—20
4—40
1—7

I want the following Output after running the Query
product_id—qty
1—11
2—6
3—10
4—10

Well, as spender ask I am trying to more clear the situation.

First of All, let’s think that I store
10 quantity of product 1
11 quantity of product 2
20 quantity of product 3
50 quantity of product 4
10 quantity of product 1 (now I have total 20 of product 1)
10 quantity of product 3 (now I have total 30 of product 3)

Secondly, let’s think that I sell
2 quantity of product 1
5 quantity of product 2
20 quantity of product 3
40 quantity of product 4
7 quantity of product 1 (now I have sold total 9 of product 1)

Thirdly, I want to know how much stock is now in my hand
11 quantity of product 1 (20-9 = 11)
6 quantity of product 2 (11-5 = 6)
10 quantity of product 3 (30-20 = 10)
10 quantity of product 4 (50-4 = 10)

My Question is: To find out this stock what is the Query?

Thanks in Advance for answering my question.

  • 1 1 Answer
  • 1 View
  • 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-05-12T12:06:23+00:00Added an answer on May 12, 2026 at 12:06 pm

    Chris’s answer is absolutely correct. But for the information I want to add this one which I found on NET.

    SELECT tunion.product_id, (  
    (IFNULL((SELECT SUM(s.qty) FROM stock s WHERE s.product_id=tunion.product_id),0))-  
    (IFNULL((SELECT SUM(p.qty) FROM sales p WHERE p.product_id=tunion.product_id),0)))  
    AS quantity   
    FROM (SELECT DISTINCT s.product_id FROM stock s  
    UNION ALL SELECT DISTINCT p.product_id FROM  sales p)  
    AS tunion GROUP BY tunion.product_id  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables like this: Person table: person_id | name | dob --------------------------------
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
hi i have tables like this: Persons: person_id, name then i have many langauge
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
If I have three different tables like this table_1 Field 1: victories Field 2:
I have three tables: videos, videos_categories, and categories. The tables look like this: videos:
I have two MySQL tables something like this: tool_owners: tool_owners_id | user_id | ...
I have 2 tables set up like this (irrelevant rows omitted): > product -
Let's say I have two tables that look like this: TH TH TH TH
I have the following model which corresponds to teh following table: PriceAvailability ID| PRODUCT_ID|

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.