Skip to content
Advertisement

Mysql query for sum two table data by product wise

If I use the below code the sum of sale and purchase product wise perfect but 5 NO product missing because no purchase only sale but I want to show all product. what-ever purchase or sale are zero.

enter image description here

If is use below code then result is like below. I don’t know sum of sale and purchase is not perfect.

enter image description here

I want result for all item sum of product wise data what ever sale or purchase made.

Advertisement

Answer

You are taking the correct approach in your first query but your JOIN conditions are wrong and you need to use LEFT JOINs instead of JOIN to get products which have no purchases or no sales:

We also use COALESCE to convert NULL values (when a product has no purchases or sales) to 0.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement