I have two tables in my database that I would like to create a view that contains all the information. One of the table holds details of each order, and the other table holds the general stuff of those orders.
Here is the details of the tables:
OrderRecords_table
OrderID|CustomerCode|Customer|Address|PickupLocation|TotalPrice|CreateTime
C00001 |AB001 |ABC Co. |CA | Store |5 |2015-01-01
C00002 |BC002 |BC Co. |CA | store |5.5 |2015-01-01
OrderDetails_table
OrderID|Company|ItemName|Amount|unitPrice|CreateTime
C00001 |ABC Co.|apple |1 |2 |2015-01-01 01:01:01
C00001 |ABC Co.|Orange |3 |3 |2015-01-01 01:01:01
C00002 |BC Co. |candy |5 |1 |2015-01-01 03:01:01
C00002 |BC Co. |pan |1 |2 |2015-01-01 03:01:01
C00002 |BC Co. |ruler |2 |2.5 |2015-01-01 03:01:01
Something like this, so on OrderRecords, orderID is unique, and orderDetails has a record for each item.
Right now I am creating a method that would need details from both tables, and it would be really confusing if I try to Select item each time across table, and I would like to create a view that contains details of both table.
I want it to be something as follow:
View V
OrderID|Company|CustomerCode|Address|ItemName|Amount|UnitPrice|CreateTime
C00001 |ABC Co.|AB001 |CA |apple |1 |2 |2015-01-01
C00001 |ABC Co.|AB001 |CA |orange |3 |3 |2015-01-01
C00002 |BC Co. |BC002 |CA |candy |5 |1 |2015-01-01
C00002 |BC Co. |BC002 |CA |pan |1 |2 |2015-01-01
C00002 |BC Co. |BC002 |CA |ruler |2 |2.5 |2015-01-01
What would the best way be to create such view? Thanks
You could use a join:
CREATE VIEW v AS
SELECT orec.OrderID,
od.Company,
orec.CustomerCode,
orec.Address,
od.ItemName,
od.Amount,
od.unitPrice,
od.CreateTime
FROM OrderRecords orec
JOIN OrderDetails od ON orec.OrderId = od.OrderId
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.