好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

oracle多表简单查询实例代码

多表查询基本语法

笛卡尔积在SQL中的实现方式既是交叉连接(Cross Join)。所有连接方式都会先生成临时笛卡尔积表,笛卡尔积是关系代数里的一个概念,表示两个表中的每一行数据任意组合。

?

1

2

-- 笛卡尔积

select * from emp, dept;

oracle多表查询

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

prompt Importing table dept...

set feedback off

set define off

insert into dept (DEPTNO, DNAME, LOC)

values (1, '01事业部' , '324' );

 

insert into dept (DEPTNO, DNAME, LOC)

values (2, '02事业部' , '234' );

 

insert into dept (DEPTNO, DNAME, LOC)

values (3, '03事业部' , '234' );

 

insert into dept (DEPTNO, DNAME, LOC)

values (4, '04事业部' , '3244' );

 

prompt Done.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

prompt Importing table emp...

set feedback off

set define off

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)

values ( '001' , '小明' , '运维' , '9000' , to_date( '26-09-2021' , 'dd-mm-yyyy' ), 900, 800, '1' );

 

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)

values ( '001' , '小刚' , 'hr' , '7000' , to_date( '26-09-2021' , 'dd-mm-yyyy' ), 900, 800, '1' );

 

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)

values ( '001' , '小花' , 'vue开发' , '6000' , to_date( '26-09-2021' , 'dd-mm-yyyy' ), 900, 800, '1' );

 

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)

values ( '001' , '小贝' , '软件工程师' , '9000' , to_date( '26-09-2021' , 'dd-mm-yyyy' ), 900, 800, '2' );

 

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)

values ( '001' , '小涛' , '数据中台' , '11000' , to_date( '26-09-2021' , 'dd-mm-yyyy' ), 900, 800, '2' );

 

insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)

values ( '001' , '小胜' , '设计' , '9000' , to_date( '26-09-2021' , 'dd-mm-yyyy' ), 900, 800, '2' );

 

prompt Done.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

prompt

 

prompt Creating table DEPT

prompt ===================

prompt

 

create table USERNAME.DEPT

(

   deptno NUMBER not null ,

   dname  VARCHAR2( 10 ) not null ,

   loc    VARCHAR2( 10 ) not null

)

tablespace USERS

   pctfree 10

   initrans 1

   maxtrans 255

   storage

   (

     initial 64K

     next 1M

     minextents 1

     maxextents unlimited

   );

 

prompt

 

prompt Creating table EMP

prompt ==================

prompt

 

create table USERNAME.EMP

(

   empno    NVARCHAR2( 20 ),

   ename    NVARCHAR2( 20 ),

   job      NVARCHAR2( 20 ),

   mgr      NVARCHAR2( 20 ),

   hiredate DATE,

   sal      BINARY_DOUBLE,

   comm     BINARY_DOUBLE,

   deptno   NVARCHAR2( 20 )

)

tablespace USERS

   pctfree 10

   initrans 1

   maxtrans 255

   storage

   (

     initial 64K

     next 1M

     minextents 1

     maxextents unlimited

   );

 

 

prompt Done

spool off

set define on

多表查询

多表查询:多表查询是指基于两个或两个以上的表或者视图的查询。

--查询员工名,工资,以及员工所在部门的名称
select   e.ename,e.sal,d.dname   from   emp e,dept d  where e.deptno=d.deptno                    
--查询部门编号为10的部门名称,员工名称,工资
select  d.dname,e.ename,e.sal  from   emp e,dept d  where e.deptno=d.deptno   and d.deptno='1' 
--查询员工名称,工资,部门名称,并且按照部门名称升序排列
select   e.ename,e.sal,d.dname  from   emp e,dept d  where e.deptno=d.deptno   order by d.dname 

--2.自连接

-- 自连接:自连接是指在同一张表上的连接查询

--查询员工的姓名以及员工所对应老板的姓名

select   e1.ename,e2.ename  from    emp  e1,emp e2 where e1.mgr=e2.empno

3.子查询(单行子查询)

子查询:子查询是指嵌入在其他sql语句中的select语句,也叫嵌套查询。

?

1

2

3

4

5

6

--查询与SMITH在同一部门的员工

select * from ( select    e.deptno from emp e,dept t where   e.deptno=t.deptno and   e.ename= 'SMITH' ) s1,emp e

where e.deptno =s1.deptno

 

 

select * from emp where deptno=( select deptno from emp where ename= 'SMITH' );

多行子查询

多行子查询是指返回多行数据的查询。

在多行子查询中使用all操作符

?

1

2

3

4

5

6

7

8

9

10

--查询与10号部门工作相同的员工名称,工作,工资,部门编号

select ename,job,sal,deptno from emp where job in ( select job from emp where deptno=1);

 

--查询比30号部门所有人工资高的员工姓名,职位,工资,部门编号

select ename,sal,job,deptno from emp where sal> all ( select sal from emp where deptno=1);

 

--在多行子查询中使用any操作符

--查询比1号部门任意人工资高的员工姓名,职位,工资,部门编号

select ename,sal,job,deptno from emp where sal> any ( select sal from emp where deptno=1);

多列子查询

单行子查询是指子查询返回单行,单列的数据

多行子查询是指子查询返回多行,单列的数据

多列子查询是指子查询返回多列数据的查询

?

1

2

3

--查询与SMITH的部门和职位完全相同的员工信息

 

select * from emp where (deptno,job)=( select deptno,job from emp where ename= 'SMITH' );

在from子句中使用子查询

在from子句中使用子查询时,该子查询会被当做一个视图来对待,因此也就叫做内嵌图,当在from子句中使用子查询时,必须给子查询起别名。(换句话说就是把子查询查出来的数据作为一张新表,在进行查询)

总结

到此这篇关于oracle多表简单查询的文章就介绍到这了,更多相关oracle多表查询内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_40074861/article/details/123627130

查看更多关于oracle多表简单查询实例代码的详细内容...

  阅读:29次