我有四张桌子
Customers
- PK:CustomerID
Events
- PK:EventID
Customers_Events
- 包含两个 FK 的连接表CustomerID
和EventID
Customer_Checkins
- 包含一个时间戳列 (CheckinDateTime
) 和一个 FK 引用CustomerID
我想要这样的输出
CustomerName EventName CheckinDateTime
------------ ---------- ---------------
Peter Christmas 2012-12-25 00:27:48.350
Peter Valentines 2013-02-14 01:19:36.113
Peter Spring 2013-05-20 02:13:53.710
问题是我得到了这个结果
CustomerName EventName CheckinDateTime
------------ ---------- ---------------
Peter Christmas 2012-12-25 00:27:48.350
Peter Christmas 2013-02-14 01:19:36.113
Peter Christmas 2013-05-20 02:13:53.710
Peter Valentines 2012-12-25 00:27:48.350
Peter Valentines 2013-02-14 01:19:36.113
Peter Valentines 2013-05-20 02:13:53.710
Peter Spring 2012-12-25 00:27:48.350
Peter Spring 2013-02-14 01:19:36.113
Peter Spring 2013-05-20 02:13:53.710
每条有效记录返回 3 次
这是我用来获得上述结果的脚本
SELECT DISTINCT
Customers.Firstname, Events.EventName, CustomerCheckins.CheckinDateTime
FROM
CustomerCheckins
INNER JOIN
Customers_Events ON CustomerCheckins.CustomerID = Customers_Events.CustomerID
LEFT OUTER JOIN
Customers ON Customers_Events.CustomerID = Customers.CustomerID
RIGHT OUTER JOIN
Events ON Customers_Events.EventID = Events.EventID
WHERE
(Customers_Events.CustomerID = 1887)
我真的很感激一些帮助来解决这个问题。我觉得我已经尝试了剧本中的每一种组合。