多表查询有几种方式


当前第2页 返回上一页

1、单行查询

1

2

3

4

5

6

7

8

9

10

11

/*--子查询(单行,多行)--*/

  

select * from emp

  

where sal > (select avg(sal) from emp)

  

/*--单行子查询可以引用(=,>,>=,<,<=,<>)操作符 --*/

  

select * from emp e

  

where e.deptno=(select d.deptno from dept d where d.deptno=1 )

2、多行查询

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

/*--多行子查询可以引用(in,any,all)操作符--*/

  

select * from emp e

  

where e.deptno in(select d.deptno from dept d)

  

/*-- any 比较返回值中的任何一个,其中一个满足,则返回true --*/

  

select * from emp e

  

where e.sal < any(select sal from emp where deptno=2)

  

/*-- all 比较返回值中的所有,全部满足,则返回true --*/

  

select * from emp e

  

where e.sal < all(select sal from emp where deptno=2)

三、聚合查询(求和,平均,记录总数)

1、求和,平均查询

1

2

3

4

5

/*--求和,平均 --*/

  

select sum(sal),avg(sal) from emp

  

select avg(nvl(sal,0)) from emp

2、记录总数

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

/*--记录总数 --*/

  

select count(*) from emp

  

select count(memo) from emp /*--统计该栏非空记录 --*/

  

select count(distinct(sex)) from emp /*--去掉重复记录 --*/

  

/*-- group by --*/

  

select e.deptno,sum(e.sal)

  

from emp e

  

group by e.deptno

  

/*-- group by having --*/

  

select e.deptno,avg(e.sal)

  

from emp e

  

group by e.deptno

  

having avg(e.sal) > 4500

相关学习推荐:mysql视频教程

以上就是多表查询有几种方式的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

详解mysql实现主从复制过程_mysql实例详解

如何在mysql中创建和删除数据库

mysql 中行转列的方法

windows下mysql5.7.10安装配置方法图文教程

oracle中多表查询如何使用natural join使用方法?

通过grep 获取mysql错误日志信息的方法代码示例

mysql适合数据仓库吗?

mysql数据库中如何进行子查询

学习mysql事件如何调用存储过程的方法

解决死锁的4种基本方法

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...