WHERE子句忽略了SQLite LEFT JOIN

时间:2013-10-03 14:05:03

标签: javascript sql sqlite left-join where

我有一个包含3个表的SQLite数据库,interestseventscategories。您可以根据自己的兴趣添加类别,我需要编写一个查询来提取附加到该类别的所有事件。

您还可以根据自己的兴趣添加活动,或者停用某个活动,以便它不会显示在某个类别下方。

e.g。对所有“大爆炸理论”剧集感兴趣,但禁用你已经看过的一集。

此查询选择兴趣表中某个类别的所有事件,包括已禁用的事件:

SELECT events.id AS event_id, events.title, event_interests.disabled
FROM interests
    JOIN events ON events.category_id = interests.id 
        AND interests.type LIKE "category" 
    LEFT JOIN interests AS event_interests ON event_interests.id = events.id 
        AND event_interests.type LIKE "event" 

这给出了这些结果:

Event_ID  Title          Disabled   
122749    Bad Education    
122815    Bad Education  1
122852    Bad Education

此查询完全相同,但限制为禁用兴趣

SELECT events.id AS event_id, events.title, event_interests.disabled
FROM interests
    JOIN events ON events.category_id = interests.id 
        AND interests.type LIKE "category" 
    LEFT JOIN interests AS event_interests ON event_interests.id = events.id 
        AND event_interests.type LIKE "event" 
WHERE event_interests.disabled = 1

仅返回已禁用的行

Event_ID  Title          Disabled    
122815    Bad Education  1

但我希望禁用所有的活动。但反转WHERE event_interests.disabled并没有给我这个:

SELECT events.id AS event_id, events.title, event_interests.disabled
FROM interests
    JOIN events ON events.category_id = interests.id 
        AND interests.type LIKE "category" 
    LEFT JOIN interests AS event_interests ON event_interests.id = events.id 
        AND event_interests.type LIKE "event" 
WHERE event_interests.disabled != 1

这会给出一个空白的结果集,而不是返回事件 122749,122852

我的JOINs做过蠢事吗?

1 个答案:

答案 0 :(得分:1)

我认为disabled列可以是NULL。 要对NULL进行测试,您必须具体:WHERE disabled IS NULL

基本上,几乎所有与NULL的比较都会产生NULL。 因此,如果你问“给我所有行禁用不是1”,数据库就无法为你提供NULL行,因为据他所知,NULL可能意味着“没有数据,但是可能是1“。

更令人惊讶的信息in the documentation

相关问题