简体   繁体   English

在Oracle中过滤查询

[英]Filter a query in Oracle

I have the following query 我有以下查询

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

There is a chance that entries in "Supervisor" table may not be present in "Employee" table as an Employee After forming the above query , i want to make "b.supervisorname" field as "null", if "b.supervisorid" not in "a.empid" column 有可能“ Supervisor”表中的条目可能不会作为“ Employee”出现在“ Employee”表中。形成上述查询后,如果“ b.supervisorid”,我想将“ b.supervisorname”字段设置为“ null”不在“ a.empid”列中

EMPLOYEE TABLE: 员工表:

EMPID--AGE--CITY--SUPERVISOR EMPID - 年龄 - 城市 - SUPERVISOR

1--12--A--123 1--12 - A - 123

2--21--B--1 2--21 - B - 1

3--23--C--2 3--23 - C - 2

Supervisor Table: 主管表:

SUPERVISOR TABLE 主管桌

EMPID--NAME EMPID - NAME

123--ABC 123 - ABC

1--EFG 1 - EFG

2-HIJ 2- HIJ

OUTPUT: OUTPUT:

EMPID--AGE--CITY--NAME EMPID - 年龄 - 城市 - NAME

1--12--A--null 1--12 - A - 空

2--21--B--ABC 2--21 - B - ABC

3--23--C--EFG 3--23 - C - EFG

i dont want to use, 我不想用

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))

as this kind of querying affects the performance 因为这种查询会影响性能

Is there any shortcut way to do it? 有什么捷径可以做到吗?

You should ALWAYS use explicit joins to avoid performance issues. 您应该始终使用显式联接以避免性能问题。 And in general it helps to define a FROM clause in queries 通常,它有助于在查询中定义FROM子句

The query below should work for you: 以下查询将为您工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM