【问题标题】:Nulls not returning in Query查询中未返回空值
【发布时间】:2018-04-11 15:29:03
【问题描述】:

我有以下查询,我想排除“否”值。我想包括 N/A、Yes 和 null。 “No”、“Yes”、“N/A”和“null”是此列中的唯一值。当我运行查询时,它不会返回空值,但会返回“N/A”和“Yes”。


select s.schoolid as "School Code", 
       sch.name as "School Name", 
       s.student_number as "Student Id", 
       s.lastfirst as "Name", 
       s.gender as "Gender", 
       s.grade_level as "Grade", 
       s.street as "Street", 
       s.city as "City", 
       s.state as "State", 
       s.zip as "Zip", 
       s.home_phone as "Home Phone" 
from u_students u 
  left join students s on u.studentsdcid = s.dcid
  left join schools sch on s.schoolid = sch.school_number
where s.grade_level in ('11','12')
  and s.enroll_status = '0'
  and U.Scs_Perm_Share_Military Not like 'No'
order by sch.name asc ;

【问题讨论】:

  • 您能否在此处粘贴一个示例,说明您的表格的外观以及它们的预期结果?
  • 将其添加到 WHERE ... OR ISNULL 假设您返回的列 U.Scs_Perm_Share_Military 具有空值
  • 谢谢大家!这解决了问题!祝你有美好的一天!
  • 您的where 条件将外部联接变为内部联接。将学生表上的这些条件从 where 移动到 join 条件。
  • 您误解了outer join,因为它返回inner join 以及其他行,但您的where 必须删除所有其他行(根据a_horse_with_no_name 评论的第一句)。了解它的作用。你没有解释你想要什么(所以请根据 mrbox 的评论编辑你的问题)所以我们不知道 a_horse_with_no_name 的评论的第二句话是否是你需要的。)

标签: sql oracle join outer-join


【解决方案1】:

如果 SELECT 语句除了测试其他值之外还测试空值,它必须包含一个 IS NULL 子句。同样正如其他人所说,您应该使用左连接条件而不是将过滤器放在 where 子句中:

select s.schoolid as "School Code", 
       sch.name as "School Name", 
       s.student_number as "Student Id", 
       s.lastfirst as "Name", 
       s.gender as "Gender", 
       s.grade_level as "Grade", 
       s.street as "Street", 
       s.city as "City", 
       s.state as "State", 
       s.zip as "Zip", 
       s.home_phone as "Home Phone" 
from u_students u 
  left join students s on u.studentsdcid = s.dcid
                      and s.grade_level in ('11','12')
                      and s.enroll_status = '0'
  left join schools sch on s.schoolid = sch.school_number
where (U.Scs_Perm_Share_Military IS NULL
       OR
       U.Scs_Perm_Share_Military <> 'No'
      )
order by sch.name asc ;

【讨论】:

  • U.Scs_Perm_Share_Military IS NULL OR 是不必要的,因为 &lt;&gt; 'No' 对于包括 NULL 在内的所有值都是 true。还假设s.s 属于where 而不是on 意味着使用left join 而不是inner join 是不好的做法。
  • technet.microsoft.com/en-us/library/ms191270(v=sql.105).aspx 查看标题为“比较空值”的部分。
  • 我明白你在说什么,很有可能他应该把 s.grade 和 s.enroll_status 放在左连接中。
  • 我们不知道他们想要什么,因为问题不清楚。这就是为什么没有minimal reproducible example 的答案应该被评论、关闭投票和反对——而不是回答。似乎他们只是想要内部连接——根据他们对OR IS NULL 有效的问题的评论。除了外部与内部连接之外,我要说的另一件事是,你只需要 `'。
猜你喜欢
  • 2022-01-01
  • 1970-01-01
  • 2016-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-09
  • 1970-01-01
  • 2021-08-04
相关资源
最近更新 更多