SQL的1999语法-ON和USING

时间:2024-02-17 15:47:04

使用自然连接时要求两张表的字段名称相同,但是如果不相同或者两张表中有两组字段是重名,这时就要利用 ON 子句指定关联条件,利用 USING 子句设置关联字段

利用 USiNG 子句设置关联字段实现自然连接

SQL> select *
  2  from emp join dept using(deptno);

    DEPTNO      EMPNO ENAME                JOB                       MGR HIREDATE              SAL       COMM DNAME
           LOC
---------- ---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ---------------------------- --------------------------
        10       7839 KING                 PRESIDENT                     17-11月-81           5000            ACCOUNTING
           NEW YORK
        10       7782 CLARK                MANAGER                  7839 09-6月 -81           2450            ACCOUNTING
           NEW YORK
        10       7934 MILLER               CLERK                    7782 23-1月 -82           1300            ACCOUNTING
           NEW YORK
        20       7902 FORD                 ANALYST                  7566 03-12月-81           3000            RESEARCH
           DALLAS
        20       7369 SMITH                CLERK                    7902 17-12月-80            800            RESEARCH
           DALLAS
        20       7566 JONES                MANAGER                  7839 02-4月 -81           2975            RESEARCH
           DALLAS
        30       7900 JAMES                CLERK                    7698 03-12月-81            950            SALES
           CHICAGO
        30       7844 TURNER               SALESMAN                 7698 08-9月 -81           1500          0 SALES
           CHICAGO
        30       7654 MARTIN               SALESMAN                 7698 28-9月 -81           1250       1400 SALES
           CHICAGO
        30       7521 WARD                 SALESMAN                 7698 22-2月 -81           1250        500 SALES
           CHICAGO
        30       7499 ALLEN                SALESMAN                 7698 20-2月 -81           1600        300 SALES
           CHICAGO

    DEPTNO      EMPNO ENAME                JOB                       MGR HIREDATE              SAL       COMM DNAME
           LOC
---------- ---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ---------------------------- --------------------------
        30       7698 BLAKE                MANAGER                  7839 01-5月 -81           2850            SALES
           CHICAGO

已选择 12 行。

利用 ON 子句设置关联条件

SQL> select *
  2  from emp e join dept d on(e.deptno=d.deptno);

     EMPNO ENAME                JOB                       MGR HIREDATE              SAL       COMM     DEPTNO     DEPTNO DNAME
                      LOC
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ---------- ---------- ---------------------------- --------------------------
      7839 KING                 PRESIDENT                     17-11月-81           5000                    10         10 ACCOUNTING
                      NEW YORK
      7782 CLARK                MANAGER                  7839 09-6月 -81           2450                    10         10 ACCOUNTING
                      NEW YORK
      7934 MILLER               CLERK                    7782 23-1月 -82           1300                    10         10 ACCOUNTING
                      NEW YORK
      7902 FORD                 ANALYST                  7566 03-12月-81           3000                    20         20 RESEARCH
                      DALLAS
      7369 SMITH                CLERK                    7902 17-12月-80            800                    20         20 RESEARCH
                      DALLAS
      7566 JONES                MANAGER                  7839 02-4月 -81           2975                    20         20 RESEARCH
                      DALLAS
      7900 JAMES                CLERK                    7698 03-12月-81            950                    30         30 SALES
                      CHICAGO
      7844 TURNER               SALESMAN                 7698 08-9月 -81           1500          0         30         30 SALES
                      CHICAGO
      7654 MARTIN               SALESMAN                 7698 28-9月 -81           1250       1400         30         30 SALES
                      CHICAGO
      7521 WARD                 SALESMAN                 7698 22-2月 -81           1250        500         30         30 SALES
                      CHICAGO
      7499 ALLEN                SALESMAN                 7698 20-2月 -81           1600        300         30         30 SALES
                      CHICAGO

     EMPNO ENAME                JOB                       MGR HIREDATE              SAL       COMM     DEPTNO     DEPTNO DNAME
                      LOC
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ---------- ---------- ---------------------------- --------------------------
      7698 BLAKE                MANAGER                  7839 01-5月 -81           2850                    30         30 SALES
                      CHICAGO

已选择 12 行。

同样,上面代码效果等同于前面所介绍的查询语句

select *
from emp,dept
where emp.deptno=dept.deptno;

不过显示结果的时候,deptno 字段只显示一次,而前面介绍的方法会显示两次