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.
From sqlcourse2.com
You can read more about
GROUP BYwith aggregate functions usage in following links:Now I think it will be simple to understand the following query which solves your problem: