Monday , 27 May 2019
Breaking News
Home > Tutorials > MySQL > How to Get SUM of Values Matching Item ID from another Table with INNER JOIN

How to Get SUM of Values Matching Item ID from another Table with INNER JOIN

Assalam-o-Alaikum!

Dear friends today we will learn about INNER JOIN and GROUP BY. Sometimes you need to calculate some value that relates to another table or you need to pull some data from another table, for this you need Joining method. Here is a problem that we will solve.

Problem:

Suppose you have two tables, one is Items and second one is “sales”. There are some entries in sale table. Now you have to calculate total sale of every item and show in list along with Item name, but the problem is that there is no “Item Name” column in sale table.

Solution:

You have to use INNER JOIN and GROUP BY methods and SUM function of MySQL to solve this problem.

How?

Let’s see, you have following tables and data.

You need this kind of result based on above table data:

Id Item Name Total Sale
1 Item 1 300
2 Item 2 400
3 Item 3 600
4 Item 4 800

 

To solve this problem use this query to get desired result:

Now run the code and see the result.

Hope you enjoyed the tutorial. See you in next tutorial.

About Muhammad Faryad

Muhammad Faryad is a professional web application developer. He has been working for 5 years in ICE786 Technologies Pvt. Ltd as a Senior Programmer and Developer. You can contact him on facebook at www.facebook.com/tolamangali

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Ad Test