I m try to make a select from one table based on register of a second table
i m using 4 tables
table Products
Product_Id | Product_Name | Product_Value | Category_Id
1 | Ball | 10 | 1
2 | Pen | 2 | 2
3 | Eraser | 1 | 1
table Categories
Category_Id | Category_Name
1 | Acessories
2 | Others
table Customers
Customer_Id | Customer_Name
1 | John
2 | Peter
table Sells
Sell_Id | Product_Id | Customer_Id | Sell_Date
1 | 1 | 1 | 2012-01-01
2 | 2 | 1 | 2012-01-02
So i want to make a select to show on customer historic what he buy
like
John Historic
Product Name Category Name Date
Ball Accessories 2012-01-01
Pen Others 2012-010
On final i am showing information form 3 different tables
I have try using INNER JOIN and LEFT JOIN but with no success.
Thanks for any help.
Break it down: THe first thing you want is a Product Name:
How do we get the category? Join it in!
Getting close! Time to bring in the order history:
Now we can bring in the name, and filter to John:
Just take it one step at a time in your mind. Build the query from each part as you go.
Since you mentioned it…
LEFT JOINis how you deal with missing data. It returns rows from the “left” table, and putsNULLin the fields of the “right” table if there is no match for theONclause.For example, if John bought a Product with no category (
Product.Category_IdwasNULL), using aJOINwould not return that record of purchase because there was no matching Category withCategory_IdNull. In that case, you couldLEFT JOIN Categoriesand in the result set,Category_Namewould beNULLas well.