在Oracle中过滤查询

时间:2014-02-19 17:58:53

标签: oracle

我有以下查询

select a.empid, a.age, a.city, b.name
  join supervisor b on a.supervisorid = b.empid

“Supervisor”表中的条目可能不会作为Employee存在于“Employee”表中 在形成上述查询后,我想将“b.supervisorname”字段设为“null”,如果“b.supervisid”不在“a.empid”列中

员工表:

  

EMPID - AGE - CITY - SUPERVISOR

     

1--12 - A - 123

     

2--21 - B - 1

     

3--23 - C - 2

主管表:

  

SUPERVISOR TABLE

     

EMPID - NAME

     

123 - ABC

     

1 - EFG

     

2-HIJ

输出:

  

EMPID - AGE - CITY - NAME

     

1--12 - A - 空

     

2--21 - B - ABC

     

3--23 - C - EFG

我不想用,

select a.empid, a.age, a.city, b.name
  from employee a
  join supervisor b on a.supervisorid =
                       (select empid
                          from supervisor
                         where empid in (select empid from employee))

因为这种查询会影响性能

有没有捷径可以做到?

1 个答案:

答案 0 :(得分:1)

您应始终使用显式联接以避免性能问题。通常,它有助于在查询中定义FROM子句

以下查询适合您:

select
    e.empid,
    e.age,
    e.city,
    s.name
FROM
    employee e
    LEFT OUTER JOIN
        supervisor s
        on e.supervisor = s.empid