使用SQL Server 2008从四个表中检索查询数据?

时间:2015-09-09 12:12:51

标签: c# sql-server

我有四张桌子:

  • Customer(ID,客户名称,城市)
  • Product(pid,pname,sprice)
  • Orders(OrderNum,CustomerID,EmpID,orderDate)
  • Sales(OrderNum,pid,qty,totalAmmount,payed,credit,CreditEndDate)

我想使用以下查询从上面的四个表中检索数据,但是我遇到了一个问题,我可以在哪里为销售表添加别名?

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]   

有任何帮助吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您需要加入Sales表格

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]