需要验证查询

时间:2016-03-15 00:02:33

标签: mysql sql database join

我有三张桌子

客户端:

客户ID 名字 姓氏

交易: Trans ID 客户端ID RepresentativeID 订单ID 代表:

代表身份证明 名字 姓氏

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

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

这是对还是我搞错了?

1 个答案:

答案 0 :(得分:0)

加盟条件很好。但是,根据OrderDate列的类型,您可能需要使用MySQL的STR_TO_DATE函数,例如:

WHERE STR_TO_DATE(`OrderDate`,'%d-%m-%Y') 
BETWEEN '1996-09-18' AND '1996-11-27'

<强>更新 我们还需要更新SELECT和FROM语法:

SELECT t.Trans_ID, c.first name, c.last_name, r.first name, r.last_name
FROM Transactions t INNER JOIN Clients c on t.ClientID= c.Client_ID
INNER JOIN Representatives r
ON t.RepresntativeID = r.Represntative_ID