简体   繁体   中英

Retrieving query data from four tables using SQL Server 2008?

I have four tables:

  • Customer (ID, CustomerName, City)
  • Product (pid, pname, sprice)
  • Orders (OrderNum, CustomerID, EmpID, orderDate)
  • Sales (OrderNum, pid, qty, totalAmmount, payed, credit, CreditEndDate)

I want to retrieve data from above four tables using the following query, but I encountered a problem where I can alias the sales table?

SELECT 
    Cs.CustomerName, Cs.City, Crs.totalAmount, p.pname, Crs.qty,
    crs.totalAmount, crs.payed, Crs.credit, ord.orderDate,  
    Crs.CreditEndDate 
FROM 
    Customer Cs 
INNER JOIN 
    Orders ord ON Cs.ID = ord.CustomerID 
INNER JOIN
    Product p ON p.pid = Sales Crs.pid 
WHERE
    ord.OrderDate BETWEEN '01/01/2014' AND '01/01/2016' 
ORDER BY 
    [CustomerName]   

Is there any help? Thanks in advance.

You need to join Sales table

SELECT Cs.CustomerName,
       Cs.City,
       s.totalAmount,
       p.pname,
       s.qty,
       s.totalAmount,
       s.payed,
       s.credit,
       ord.orderDate,
       s.CreditEndDate
FROM   Customer Cs
       INNER JOIN Orders ord
               ON Cs.ID = ord.CustomerID
       INNER JOIN Sales s
               ON s.OrderNum = ord.OrderNum
       INNER JOIN Product p
               ON p.pid = s.pid
WHERE  ord.OrderDate BETWEEN '01/01/2014' AND '01/01/2016'
ORDER  BY [CustomerName] 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM