温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

join和on语句中select语句选择公共列的区别

发布时间:2020-05-31 15:18:21 来源:网络 阅读:545 作者:逛街的刀客 栏目:关系型数据库

  对多个表进行join时,在select语句中,如果使用using语句,则using语句中选中的列,在select语句中不能指定限定词,否则会报ORA-25154

查看emp表

SQL> select * from emp;
EMPNO ENAME      JOB         MGR HIREDATE          SAL      COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
 7369 SMITH      CLERK      7902 1980/12/17     800.00               20
 7499 ALLEN      SALESMAN   7698 1981/2/20     1600.00    300.00     30
 7521 WARD       SALESMAN   7698 1981/2/22     1250.00    500.00     30
 7566 JONES      MANAGER    7839 1981/4/2      2975.00               20
 7654 MARTIN     SALESMAN   7698 1981/9/28     1250.00   1400.00     30
 7698 BLAKE      MANAGER    7839 1981/5/1      2850.00               30
 7782 CLARK      MANAGER    7839 1981/6/9      2450.00               10
 7788 SCOTT      ANALYST    7566 1987/4/19     3000.00               20
 7839 KING       PRESIDENT       1981/11/17    5000.00               10
 7844 TURNER     SALESMAN   7698 1981/9/8      1500.00      0.00     30
 7876 ADAMS      CLERK      7788 1987/5/23     1100.00               20
 7900 JAMES      CLERK      7698 1981/12/3      950.00               30
 7902 FORD       ANALYST    7566 1981/12/3     3000.00               20
 7934 MILLER     CLERK      7782 1982/1/23     1300.00               10

查看dept表

SQL> select * from dept;
DEPTNO DNAME          LOC
------ -------------- -------------
    10 ACCOUNTING     NEW YORK
    20 RESEARCH       DALLAS
    30 SALES          CHICAGO
    40 OPERATIONS     BOSTON

在select语句中使用using语句中指定的列,添加限定词

SQL> select e.deptno,e.sal,d.dname from emp e join dept d using(deptno)
ORA-25154: USING 子句的列部分不能有限定词

不添加时

SQL> select deptno,e.sal,d.dname from emp e join dept d using(deptno);
DEPTNO       SAL DNAME
------ --------- --------------
    10   2450.00 ACCOUNTING
    10   5000.00 ACCOUNTING
    10   1300.00 ACCOUNTING
    20   2975.00 RESEARCH
    20   3000.00 RESEARCH
    20   1100.00 RESEARCH
    20    800.00 RESEARCH
    20   3000.00 RESEARCH
    30   1250.00 SALES
    30   1500.00 SALES
    30   1600.00 SALES
    30    950.00 SALES
    30   2850.00 SALES
    30   1250.00 SALES
14 rows selected

而使用on时,则必须指定限定词才能正确的显示,否则会报错,提示deptno未能识别是哪个表,因为dept和emp表中都有deptno列

 select deptno,e.sal,d.dname from emp e join dept d on(e.deptno=d.deptno);
select deptno,e.sal,d.dname from emp e join dept d on(e.deptno=d.deptno)
ORA-00918: 未明确定义列

给deptno添加限定词,就可以正常显示了

SQL> select e.deptno,e.sal,d.dname from emp e join dept d on(e.deptno=d.deptno);
DEPTNO       SAL DNAME
------ --------- --------------
    10   2450.00 ACCOUNTING
    10   5000.00 ACCOUNTING
    10   1300.00 ACCOUNTING
    20   2975.00 RESEARCH
    20   3000.00 RESEARCH
    20   1100.00 RESEARCH
    20    800.00 RESEARCH
    20   3000.00 RESEARCH
    30   1250.00 SALES
    30   1500.00 SALES
    30   1600.00 SALES
    30    950.00 SALES
    30   2850.00 SALES
    30   1250.00 SALES
14 rows selected


在使用using时,对select语句中的选定using指定的列时,无需指定限定词

在使用on时,必须在select语句中对on语句条件中的条件列添加限定词。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI