如何将多个普通连接语句重写为多个INNER JOIN语句?

时间:2018-02-06 06:11:44

标签: sql ms-access

这是具有多个连接的SQL查询,如下所示:

SELECT H1.horse_name AS Horse, H2.horse_name AS Sire, event_name AS Event
FROM Horse AS H1, Horse AS H2, Event, Entry, Entry AS SireEntry
WHERE H1.horse_sire = H2.horse_id
AND H1.horse_id = Entry.horse_id
AND H2.horse_id = SireEntry.horse_id
AND Event.event_id = Entry.event_id
AND Event.event_id = SireEntry.event_id;

我试图用多个INNER JOINS而不是普通连接重写的同一个SQL语句。但是我无法获得与普通连接相同的结果。 这是我的尝试:

SELECT H1.horse_name AS Horse, H2.horse_name AS Sire, event_name AS Event
FROM ((((Horse AS H1 INNER JOIN Horse AS H2 ON H1.horse_sire = H2.horse_id)
INNER JOIN Entry ON H1.horse_id = Entry.horse_id)
INNER JOIN Entry AS SireEntry ON H2.horse_id = SireEntry.horse_id)
INNER JOIN Event ON Event.event_id = SireEntry.event_id);

那么如何使用多个INNER JOINS重写相同的SQL查询语法以获得与普通连接相同的结果? 如果提供语法将会有所帮助。

1 个答案:

答案 0 :(得分:1)

加入Event时,您忘记了一个条件:

...
INNER JOIN Entry AS SireEntry ON H2.horse_id = SireEntry.horse_id)
INNER JOIN Event ON Event.event_id = SireEntry.event_id 
                AND Event.event_id = Entry.event_id);