Reputation: 387
I have 2 tables, customers and orders, i would like to display all orders for each customer on a page. From orders when there are 2 orders belonging to same customer id i would like combine them into 1 div so there are shown together. Do i need to create 2 queries and loops to achieve this? E.g. 1 loop to get orders and another loop inside it, check if order id is a duplicate get the record. Please advise me and give me any ideas you might have thanks in advance.
Sample Tables
Customer Table
Customer ID, Customer Name
1, Jason
2, Joe
Order Table
Order ID, Customer ID, Order Details
1, 1, Pen
2, 1, Watch
3, 2, Mirror
What I would like to achieve:
Customer_ID, Customer_Name, Customer_Order
1, Jason,
Pen, Watch
2, Joe,
Mirror.
Upvotes: 0
Views: 357
Reputation: 133380
you can use join for obtain all the info you need
eg for customer 1
select c.Customer_ID, c.Customer_Name, o.Customer_Order
from Customer c
inner join Order o on o.Customer_ID = c.Customer_ID
where and c.Customer_ID = 1
Upvotes: 1