SQL Joins question

I have three tables which all have the following data:-

table 1
warehouse
product
purchases_thismonth
purchases_nextmonth
purchases_monthafter

table2
warehouse
product
orders_thismonth
orders_nextmonth
orders_monthafter

table3
warehouse
product
quotes_thismonth
quotes_nextmonth
quotes_monthafter

I want to create a query which basically pulls my data together into the following format

warehouse,product,purchases_thismonth,purchases_nextmonth,purchases_monthafter,orders_thismonth,orders_nextmonth,orders_monthafter,quotes_thismonth,quotes_nextmonth,quotes_monthafter

I have tried to do this but I keep getting multiple lines for each product.
Any ideas what I am doing wrong or indeed what I should be doing
sagarhAsked:
Who is Participating?
 
Renante EnteraSenior PHP DeveloperCommented:
Hi sagarh!

I think this is what you are looking for :

Select
  t1.warehouse, t1.product, t1.purchases_thismonth, t1.purchases_nextmonth, t1.purchases_monthafter,
  t2.orders_thismonth, t2.orders_nextmonth, t2.orders_monthafter,
  t3.quotes_thismonth, t3.quotes_nextmonth, t3.quotes_monthafter
From  Table1 t1
Left Join Table2 t2
  On t2.warehouse = t1.warehouse And t2.product = t1.product
Left Join Table3 t3
  On t3.warehouse = t1.warehouse And t3.product = t1.product

Hope this helps you.  Just try it.


Goodluck!
eNTRANCE2002 :-)
0
 
riaancorneliusCommented:
>
Use inner joins.
0
 
riaancorneliusCommented:
example:
SELECT table1.warehouse, table1.product, table2.orders_thismonth
FROM table1 INNER JOIN table 2
ON table1.product = table2.product
0
 
Renante EnteraSenior PHP DeveloperCommented:
Hi sagarh!

I'm glad that I have helped you.  Hope to help you again.


Regards!
eNTRANCE2002 :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.