如何在SQL中实现这种双连接?

Len*_*oyt 1 sql sqlite join inner-join

我有这个查询,它连接存储在缓存表中的事件的名称.

SELECT OccurrenceCache.occurrence_date, CalendarItem.summary FROM OccurrenceCache
    INNER JOIN CalendarItem ON CalendarItem.ROWID = OccurrenceCache.event_id
    WHERE OccurrenceCache.occurrence_date >= (strftime('%s', 'now', 'localtime', 'start of day') - strftime('%s', '2001-01-01', 'start of day')) AND OccurrenceCache.occurrence_end_date <= (strftime('%s', 'now', 'localtime', 'start of day') - strftime('%s', '2001-01-01', 'start of day') + 24 * 60 * 60);
Run Code Online (Sandbox Code Playgroud)

现在我想要包含事件的位置数据,这些数据存储在名为Location的表中.CalendarItem.location_id引用位置条目(0表示未指定位置).我用另一个JOIN语句尝试了它,但它不起作用:

SELECT OccurrenceCache.occurrence_date, CalendarItem.summary, Location.title FROM OccurrenceCache
    INNER JOIN CalendarItem ON CalendarItem.ROWID = OccurrenceCache.event_id
    INNER JOIN Location ON Location.ROWID = CalendarItem.location_id
    WHERE OccurrenceCache.occurrence_date >= (strftime('%s', 'now', 'localtime', 'start of day') - strftime('%s', '2001-01-01', 'start of day')) AND OccurrenceCache.occurrence_end_date <= (strftime('%s', 'now', 'localtime', 'start of day') - strftime('%s', '2001-01-01', 'start of day') + 24 * 60 * 60);
Run Code Online (Sandbox Code Playgroud)

它返回0结果.

Muh*_*ani 5

如果表中的所有条目与Location表不匹配calenderitem,则使用LEFT JOIN替代方案将是解决方案.

SELECT OccurrenceCache.occurrence_date, CalendarItem.summary, Location.title FROM OccurrenceCache
    INNER JOIN CalendarItem ON CalendarItem.ROWID = OccurrenceCache.event_id
    LEFT JOIN Location ON Location.ROWID = CalendarItem.location_id
    WHERE OccurrenceCache.occurrence_date >= (strftime('%s', 'now', 'localtime', 'start of day') - strftime('%s', '2001-01-01', 'start of day')) AND OccurrenceCache.occurrence_end_date <= (strftime('%s', 'now', 'localtime', 'start of day') - strftime('%s', '2001-01-01', 'start of day') + 24 * 60 * 60);
Run Code Online (Sandbox Code Playgroud)

在SQL中使用外连接