前言
提示:这里可以添加本文要记录的大概内容:
数据库很重要,这里介绍几种sql多表查询是方式
提示:以下是本篇文章正文内容,下面案例可供参考
一、Mysql是什么?
MySQL 是一个关系型数据库,使用 SQL 语言进行增删改查操作,目前属于 Oracle 旗下的产品。
MySQL 数据库开源免费,能够跨平台,支持分布式,性能也不错,可以和 PHP、Java 等 Web 开发语言完美配合,非常适合中小型企业作为 Web 数据库(网站数据库)。
二、查询的几种方式
1.传统方式
代码如下(示例):
传统连接方式
1 select * from dept a, emp b where a.name = b.name
只有一个同名栏位时用natural join
2 select * from dept a natural join emp b
有多个同名栏位时用 using()
3 selsct * from dept a join emp n using(name)
左外连接 记录左表的全部记录
4 select * from student a left outer join teacher b on a.name = b.name
select * from dept a, emp b where a.name = b.name(+)
右外连接 记录右表的全部记录
5 select * from student a right outer join teacher b on a.name = b.name
select * from dept a, emp b where a.name(+) = b.name
完全外连接 记录所有记录
6 selsct * from dept a full outer join emp b on a.name = b.name
2.子查询(单,多行)
代码如下(示例):
单行查询 可以引用 (= > >= < <= <>) 操作符
select * from emp where sal > (select avg(sal) from emp)
select * from emp a where e.deptno = (select b.deptno from dept b where b.deptno = 1)
多行查询
select * from emo a where e.deptno in(select b.deptno from dept b)
any多行查询 比较返回值满足其中一个则返回true
select * from emp a where e.sal < any(select sal from emp where deptno = 2)
all多行查询 比较返回值全部满足则返回true
select * from emp a where a.sal < all(select sal from emp where deptno = 2)
3.聚合查询(求和,平均,统计)
求和
select sum(sal),avg(sal) from emp
select avg(nvl(sal,0)) from emp
统计记录总数
select count(*) from emp
指定字段非空记录总数
select count(memo) from emp
指定字段非空去重复记录总数
select count(distinct(sex)) from emp
分组
select * from emp e grop by e.deptno
select * from emp e grop by e.deptno having avg(e.sal) > 4500
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了sql多表查询的几种方式