1

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

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

现在我也想包含事件的位置数据,这些数据存储在名为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);

它返回 0 个结果。

4

1 回答 1

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

在 SQL 中使用外部联接

于 2013-02-16T11:04:01.433 回答