多表查询有几种方式


当前第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如何实现自增序列

php连接mysql数据库方法简化版

mysql怎么创建索引

mysql官网如何下载源码包?

mysql建表语句是什么

mysql的内外查询分别是什么?

mysql 数据库函数库

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


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

数据库系统概念 第6版

机械工业出版社

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



打赏

取消

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

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

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

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

评论

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