Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining tables on foreign key

I have the following three tables:

  • Product
  • Purchase (contains ProductID as foreign key)
  • Sale (contain ProductID as foreign key as well)

I want to form query joining these 3 tables where my result is the product name, purchased, and sold.

Product Name - (from Products table)

Purchased - (Number of occurences in Purchase table according to ProductID)

Sold - (Number of occurences in Sale Table according to ProductID)

Can you please set me on the right track by giving me hints and I'll complete by myself?

like image 855
Hussein Yassine Avatar asked Dec 06 '25 08:12

Hussein Yassine


1 Answers

I'm betting this will get deleted...but hopefully you see this before it does. The following is really helpful in understanding the differences in the SQL JOINS. SQL JOINS. This answer or Kyle's answers is all you need to solve your question.

Source: INNER JOIN, LEFT/RIGHT OUTER JOIN

like image 79
Kevin Vasko Avatar answered Dec 08 '25 21:12

Kevin Vasko