NOT NULL本身可以正确输出。添加NOT NULL,AND和OR输出null

时间:2019-03-20 15:25:13

标签: sql

如何将NOT NULLANDOR结合起来?

此查询的输出正确。它从rate_spread列中输出非null:

SELECT action_taken, action_taken_name, applicant_ethnicity, 
       applicant_ethnicity_name, applicant_income_000s, county_name, 
       loan_amount_000s, rate_spread
FROM usa_cfpb_hmda.2007

WHERE rate_spread is NOT NULL
LIMIT 10

enter image description here

但是,添加约束后,Not Null不再起作用。但是OR确实可以工作。如本例所示:

SELECT action_taken, action_taken_name, 
       applicant_ethnicity, applicant_ethnicity_name, 
       applicant_income_000s, county_name, loan_amount_000s, 
       rate_spread
FROM usa_cfpb_hmda.2007

where rate_spread is NOT NULL
     and action_taken = 1
     OR action_taken = 6

限制10

enter image description here

2 个答案:

答案 0 :(得分:2)

您必须确定逻辑的优先级(即使用括号):

where rate_spread is not null and 
     (action_taken = 1 or action_taken = 6);

答案 1 :(得分:0)

我相信您需要用括号将您的病情分组

SELECT action_taken, action_taken_name, 
       applicant_ethnicity, applicant_ethnicity_name, 
       applicant_income_000s, county_name, loan_amount_000s, 
       rate_spread
FROM usa_cfpb_hmda.2007

where rate_spread is NOT NULL
     and (action_taken = 1
     OR action_taken = 6)

因此,SQL将条件解释为:在action_taken = 1或action_taken = 6中,并且rate_spread不为空。