How do I write an sql query to display items from 2 tables based on a selection of a criteria which is from a different table.
Tables I have:
- Customer table has columns CustomerID, Name, Address, Tel
- CustomerOrder table has columns CustomerID, OrderID, Date, TotalAmount, Status
- OrderItem table has columns OrderID, ProductCode, UnitPrice, Qty, TotalPrice
So when a CustomerID is selected, I want the orders to be displayed joining these 3 tables. so as below it should display all the orders that the customer has ever placed.
I tried using the query:
Select CustomerOrder.*, OrderItem.*
From CustomerOrder
INNER JOIN OrderItem Where Customer.CustomerID = $CustomerID
But it’s not working. Need help in the query and also in displaying the data properly using php.
Can anyone help?
E.g.
CustomerID:__________
OrderID:__1____ Date:______ TotalAmount:______ Status:_____
ProductCode:__ UnitPrice:___ Qty:_____TotalPrice:__________
ProductCode:___ UnitPrice:______ Qty:_____ TotalPrice:_________
OrderID:___2___ Date:______ TotalAmount:______ Status:_____
ProductCode:__ UnitPrice:___ Qty:_____TotalPrice:__________
ProductCode:___ UnitPrice:______ Qty:_____ TotalPrice:_________
Since you don’t need to display Customer information at this step, then you query can be :
As you’d like to no repeat order information in your output, your PHP code should be soemthing like this:
Anothere way of doing the same is to first get all the Orders information from
CustomerOrderonly.And then loop on the result and get items infotmation
OrderItemfor each order.