2016-03-14 126 views
0

非常感謝...這是我的最後一個問題:JOIN SQL查詢日期

我有三個表有以下欄目:

  1. 客戶:

客戶名稱姓氏

  1. 個交易:

反式ID的ClientID RepresentativeID訂購日期

  • 代表:
  • 代表ID名姓

    I需要顯示的所有交易信息,以及在特定日期發生的代表名稱和客戶名稱。這個查詢是否正確?

    SELECT * 
    FROM [Transactions], Clients.first name, Clients.last name,  Representatives.first name, Representatives. last name 
    INNER JOIN [Clients] 
    ON Transactions.ClientID= Clients.Client ID 
    INNER JOIN [Representatives] 
    ON Transactions.RepresntativeID = Representatives.Represntative ID 
    WHERE Transactions.OrderDate BETWEEN '1996-09-18' AND '1996-11-27'; 
    

    這是正確的還是我得到了所有錯誤?

    回答

    3

    WHEREONORDER BY

    ;在去年底沒有中間

    SELECT * 
    FROM [Orders] 
    JOIN [Customers] 
        ON Orders.CustomerID = Customers.CustomerID 
    WHERE Orders.OrderDate BETWEEN '1996-09-18' AND '1996-11-27' 
    ORDER BY Customers.CustomerName; 
    
    +0

    非常感謝......這是我的最後一個問題: – OBZ

    0
    SELECT Clients.[first NAME] AS ClientFirstName 
         ,Clients.[last NAME] AS ClientLastName 
         ,Representatives.[first NAME] AS RepresentativesFirstName 
         ,Representatives.[last NAME] AS RepresentativesLastName 
         ,Transactions.* 
    FROM [Transactions] 
    INNER JOIN [Clients] 
        ON Transactions.ClientID = Clients.Client ID 
    INNER JOIN [Representatives] 
        ON Transactions.RepresntativeID = Representatives.Represntative ID 
    WHERE Transactions.OrderDate BETWEEN '1996-09-18' 
         AND '1996-11-27'; 
    

    您預期的結果列應該把什麼它SELECT之前。 From是你的表或數據集。

    另外,您可以使用別名來簡化SQL,如下所示。

    SELECT c.[first NAME] AS ClientFirstName 
         ,c.[last NAME] AS ClientLastName 
         ,r.[first NAME] AS RepresentativesFirstName 
         ,r.[last NAME] AS RepresentativesLastName 
         ,t.* 
    FROM [Transactions] t 
    INNER JOIN [Clients] c 
        ON t.ClientID = c.Client ID 
    INNER JOIN [Representatives] r 
        ON t.RepresntativeID = r.Represntative ID 
    WHERE t.OrderDate BETWEEN '1996-09-18' 
         AND '1996-11-27';