MQL多表查询的几种方式

文章目录


前言

提示:这里可以添加本文要记录的大概内容:

数据库很重要,这里介绍几种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多表查询的几种方式

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值