简体   繁体   English

这两个Oracle查询有何不同?

[英]How are these two Oracle queries different?

How oracle executes these two queries with different results oracle如何执行这两个具有不同结果的查询

1. 1。

SELECT e.ename,
       m.ename
FROM   emp e,
       emp m
WHERE  e.empno = m.mgr;

2. 2。

SELECT e.ename,
       m.ename
FROM   emp e,
       emp m
WHERE  m.empno = e.mgr; /*<-- Different*/

Not getting the clear result... 没有得到明确的结果...

Thank you, all of you in advance . 谢谢大家。

The first query shows managers and their employees, for example BLAKE is the manager for 5 staff. 第一个查询显示经理及其员工,例如BLAKE是5名员工的经理。 The second shows employees and their managers, so BLAKE appears five times in the second column. 第二个显示雇员及其经理,因此BLAKE在第二列中出现了五次。

I think they could be written more clearly, as: 我认为可以将它们写得更清楚,例如:

-- Managers and their employees:
select m.ename as manager
     , e.ename as employee
from   emp m
       join emp e on e.mgr = m.empno
order by 1,2;

MANAGER    EMPLOYEE
---------- ----------
BLAKE      ALLEN
BLAKE      JAMES
BLAKE      MARTIN
BLAKE      TURNER
BLAKE      WARD
CLARK      MILLER
FORD       SMITH
JONES      FORD
JONES      SCOTT
KING       BLAKE
KING       CLARK
KING       JONES
SCOTT      ADAMS

13 rows selected

-- Employees and their managers:
select e.ename as employee
     , m.ename as manager
from   emp e
       join emp m on m.empno = e.mgr
order by 1,2;

EMPLOYEE   MANAGER
---------- ----------
ADAMS      SCOTT
ALLEN      BLAKE
BLAKE      KING
CLARK      KING
FORD       JONES
JAMES      BLAKE
JONES      KING
MARTIN     BLAKE
MILLER     CLARK
SCOTT      JONES
SMITH      FORD
TURNER     BLAKE
WARD       BLAKE

13 rows selected

Those are two different queries: 这是两个不同的查询:

  • The first one lists the employees (left) with their managers (right). 第一个列出了雇员(左)和他们的经理(右)。

  • The second one lists the managers (left) with their employees (right). 第二个列出了经理(左)和员工(右)。

You need to decide which one you want. 您需要决定要哪个。

Also, you should really use the JOIN keyword when joining two tables (it's not the 90s anymore). 另外,在连接两个表时,您应该真正使用JOIN关键字(现在不再是90年代了)。 Your first query will look like: 您的第一个查询将如下所示:

SELECT e.ename,
       m.ename
  FROM emp e
  JOIN emp m on e.empno = m.mgr;

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

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