按日期

时间:2016-03-14 22:23:03

标签: mysql sql sql-server join

非常感谢...这是我的最后一个问题:

我有三个包含以下列的表:

  1. 客户:
  2.   

    客户端ID名字姓氏

    1. 交易:
    2.   

      Trans ID ClientID RepresentativeID OrderDate

      1. 代表:
      2.   

        代表身份证名字姓氏

        我需要显示所有交易信息,以及在特定日期发生的代表名称和客户名称。这个查询对吗?

        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';
        

        这是对还是我搞错了?

2 个答案:

答案 0 :(得分:3)

WHERE追踪ON之后ORDER 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;

答案 1 :(得分: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';

您期望的结果列应该将其放到SELECTFrom是您的表格或数据集。

另外,您可以使用别名来简化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';