【问题标题】:JOIN SQL Query by dateJOIN SQL 按日期查询
【发布时间】:2016-06-30 04:22:37
【问题描述】:

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

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

  1. 客户:

客户 ID 名字 姓氏

  1. 交易:

Trans ID 客户 ID 代表 ID 订单日期

  1. 代表:

代表身份证名姓氏

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

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

这是对的还是我全错了?

【问题讨论】:

    标签: mysql sql sql-server join


    【解决方案1】:

    WHEREON 之后和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;
    

    【讨论】:

    • 非常感谢...这是我的最后一个问题:
    【解决方案2】:
    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';
    

    【讨论】:

      猜你喜欢
      • 2021-11-15
      • 2014-01-11
      • 2019-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 2019-03-01
      相关资源
      最近更新 更多