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 6908863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:36:59+00:00 2026-05-27T08:36:59+00:00

I want to join 2 databases together and work out the sum of each

  • 0

I want to join 2 databases together and work out the sum of each one with the same ID from both tables, if you get where im coming from…

I have one database with ID, product_name, product_description
and the other has ID, stock amount, date_added

i have joined the databases together using the JOIN function and it displays them correctly, but for each entry into the database it adds another row when i add stock and shows another row at the frontend. I would like it to group all of the same id’s into one row.

I have tried this:

$result = mysql_query("SELECT * FROM site_products JOIN site_trans ON site_products.product_id = site_trans.trans_product GROUP BY site_products.product_id");

it groups the same id’s together into one row, but the stock amount just displays the last amount added, not the total amount.

i was thinking about adding the SUM() to this but im not sure where it would go in the query.

  • 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-05-27T08:36:59+00:00Added an answer on May 27, 2026 at 8:36 am

    From sqlcourse2.com

    The GROUP BY clause will gather all of the rows together that contain data in the
    specified column(s) and will allow aggregate functions to be performed on the 
    one or more columns.
    

    You can read more about GROUP BY with aggregate functions usage in following links:

    • w3schools.com
    • SQL: GROUP BY Clause
    • GROUP BY clause
    • MySQL GROUP BY – Aggregate Functions

    Now I think it will be simple to understand the following query which solves your problem:

    SELECT 
        site_products.product_id, 
        SUM(stock amount) AS total_amount
        FROM site_products JOIN site_trans 
                ON site_products.product_id = site_trans.trans_product 
        GROUP BY site_products.product_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to join two tables using JPQL : SELECT * FROM A LEFT
I want to join two tables (below) which i get. But then I want
I have two tables that I want to join together. Table1 Year, ID, Theme,
I am trying to join two tables. One being the sys.databases table and the
Possible Duplicate: Combining several database table together? I want from two database table get
I have this situation where i want to get data from 3 different databases,
I want to join two strings each representing a relative URL in Javascript. I
I want to join two tables with several hundred columns, like this: select *
I want to join my CHEESE table with FRESHNESS to get CHEESE and FRESHNESS
I have 4 tables and i want to join them and extarct 4 values.

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.