oracle 我的选择查询正在删除空值行

abithluo  于 2022-11-03  发布在  Oracle
关注(0)|答案(1)|浏览(131)

因此,我的oracle查询将使用customer和emp表显示所有客户ID、客户名称和销售代表名称,但前提是销售代表名称的最后一个字符或最后一个字符之前不包含R,并且列出所有客户,即使他们没有销售代表。因此,我编写了以下查询-

select c.custid "Customer  ID"
,c.name "Customer Name"
,s.ename "Sales Rep"
from customer c
left outer join emp s
on c.repid = s.empno
where 
(s.ename not like '%R'
and s.ename not like '%R_')
order by c.name ;

此查询应给予以下结果-x1c 0d1x
但是,它给我下面的结果。它正在删除空值记录,即使我使用了左连接!我不明白为什么!

救命啊!
问候你Layla

jyztefdp

jyztefdp1#

将滤波器置于JOIN条件下:

select c.custid AS "Customer  ID"
,      c.name   AS "Customer Name"
,      s.ename  AS "Sales Rep"
from   customer c
       left outer join emp s
       on (   c.repid = s.empno
          AND s.ename not like '%R'
          AND s.ename not like '%R_')
order by c.name;

如果在WHERE子句中有筛选器,则s.ename必须是非NULL,并且它会有效地将LEFT OUTER JOIN转换为INNER JOIN
或者,您可以在WHERE子句中使用筛选器,并添加另一个筛选器来检查NULL值:

select c.custid AS "Customer  ID"
,      c.name   AS "Customer Name"
,      s.ename  AS "Sales Rep"
from   customer c
       left outer join emp s
       on (c.repid = s.empno)
WHERE (   s.ename not like '%R'
      AND s.ename not like '%R_')
OR    s.empno IS NULL             -- Check for outer joined rows
order by c.name;

但通常将这些过滤器放在连接条件中会更容易理解。

相关问题