一、内连接【inner join】

  1. 语法一:select 列名 from 表1 inner join 表2 on 表1.公共字段=表2.公共字段

  2. 语法二:select 列名 from 表1,表2 where 表1.公共字段=表2.公共字段

  3. 注意:显示公共字段需要指定表名,否则会报错

    select stuinfo.stuno,stuname from stuinfo inner join stumarks on stuinfo.stuno=stumarks.stuno;

  4. 三表查询

    select * from 表1 inner join 表2 on 表1.公共字段=表2.公共字段 inner join 表3 on 表2.公共字段=表3.公共字段

二、外连接

  1. 左外连接(left join)

    a. 以左边的表为标准,如果右边的表没有对应的记录,用null填充

    b. 语法:select 列名 from 表1 left join 表2 on 表1.公共字段=表2.公共字段

  2. 右外连接(right join)

    a. 以右边的表为标准,如果左边的表没有对应的记录,用null填充

    b. 语法:select 列名 from 表1 right join 表2 on 表1.公共字段=表2.公共字段    

  3. 交叉连接(cross join)

    a. 如果没有连接表达式返回的事笛卡尔积

      select * from t1 cross join t2;

    b. 如果有连接表达式等价于内连接

      select * from t1 cross join t2 where t1.id=t2.id;

  4. 自然连接(natural join)

    a. 自动地判断连接条件,它是通过同名字段来判断的

    b. 自然连接分为:

      1) 自然内连接:natural join

      2) 自然左外连接 natural left join

      3) 自然右外连接 natural right join

  5. 指定连接字段:using()

    a. using也会对连接字段进行整理,整理方式和自然连接是一致的。

三、子查询

  1. 语法:select 语句 where 条件 (select ... from 表)

  2. 外面的查询称为父查询,括号中的查询称为子查询

  3. 子查询为父查询提供条件

    a. 找出笔试80分的学生

      select * from stuinfo where stuno=(select stuno from stumaks where writtenexam=80);

    b. 找出笔试最高分的一个学生

      select * from stuinfo where stuno=(select stuno from stumaks order by writtenexam desc limit 1);

      select * from stuinfo where stuno=(select stuno from stumarks where writtenexam=(select max(writtenexam) from stumarks));

  4. in | not in 子查询

    a. 用于子查询的返回结果多个值。

    b. 查找笔试成绩及格的同学

      select * from stuinfo where stuno in (select stuno from stumarks where writtenexam >= 60);

    c. 查找不及格的同学

      select * from stuinfo where stuno in (select stuno from stumarks where writtenexam < 60);

    c. 查找没有通过的同学

      select * from stuinfo where stuno not in (select stuno from stumarks where writtenexam >= 60);

  5. exists 和 not exists

    a. 如果有人超过80分就显示所有的学生

      select * from stuinfo where exists (select * from stumarks where writtenexam >= 80);

    b.  如果没有人超过80分就显示所有学生

      select * from stuinfo where not exists (select * from stumarks where writtenexam >= 80);

  6. 子查询分类

    a. 标量子查询:子查询返回的结果只有一个

    b. 列子查询:子查询返回的结果是一个列表

    c. 行列子查询:子查询返回的结果是一行

      ex1:查询成绩最高的男生和女生

        select stuname,stusex,ch from stu where (stusex,ch) in (select stusex,max(ch) from stu group by stusex)

    d. 表子查询:子查询返回的结果当成一个表

      ex1:

        select stuname,stusex,ch from (select * from stu order by ch desc) as t group by stusex;

      注意:from 后面是一个表,如通子查询的结果当成表来看,必须将子查询的结果取别名。

四、union 联合

  1. 作用将多个select语句结果集纵向联合起来

  2. 语法:select 语句 union [选项] select 语句 union [选项] select 语句

    a. select stuno,stuname from stu union select id,name from Go1;

  3. 注意:

    a. union两端的select语句的字段个数必须一致

    b. union两端的select语句字段名可以不一致,最终按第一个select语句的字段名

    c. union两端的select语句中的数据类型可以不一致。

  4. 例题:

    a. 查找上海的男生和北京的女生

      select stuname,stuaddr,stusex from stu where (stuaddr='上海' and stusex='男') or (stuaddr='北京' and stusex='女');

      select stuname,stuaddr,stusex from stu where stuaddr='上海' and stusex='男' union select stuname,stuaddr,stusex from stu where stuaddr='北京' and stusex='女'

  5. union选项

    a. all:显示所有数据

    b. distinct:去除重复数据【默认】

      select name from go1 union all select name from stu;

mysql学习笔记--数据库多表查询的更多相关文章

  1. mysql学习笔记--数据库单表查询

    一.查询语句 1.  select [选项] 列名 [from 表名]  [where 条件]  [order by 排序]  [group by 分组]  [having 条件]  [limit 限 ...

  2. MySQL学习笔记8——多表查询

    多表查询 多表查询 *合并结果集 *连接查询 *子查询 合并结果集 *要求被合并的表中,列的类型和列数相同(实际上是查询的结果集列类型和列数相同即可) *UNION,去除重复行 *UNION ALL, ...

  3. MySQL学习笔记-数据库文件

    数据库文件 MySQL主要文件类型有如下几种 参数文件:my.cnf--MySQL实例启动的时候在哪里可以找到数据库文件,并且指定某些初始化参数,这些参数定义了某种内存结构的大小等设置,还介绍了参数类 ...

  4. MySQL学习笔记-数据库内存

    数据库内存 InnoDB存储引擎内存由以下几个部分组成:缓冲池(buffer pool).重做日志缓冲池(redo log buffer)以及额外的内存池(additional memory pool ...

  5. MySQL学习笔记-数据库后台线程

    数据库后台线程 默认情况下讲述的InnoDB存储引擎,以后不再重复声明.后台线程有7个--4个IO thread,1个master thread,1个锁监控线程,1个错误监控线程.IO thread的 ...

  6. Mybatis学习笔记之---多表查询(1)

    Mybatis多表查询(1) (一)举例(用户和账户) 一个用户可以有多个账户 一个账户只能属于一个用户(多个账户也可以属于同一个用户) (二)步骤 1.建立两张表:用户表,账户表,让用户表和账户表之 ...

  7. Mybatis学习笔记之---多表查询(2)

    Mybatis多表查询(2) (一)举例 用户和角色 一个用户可以有多个角色,一个角色可以赋予多个用户 (二)步骤 1.建立两张表:用户表,角色表,让用户表和角色表具有多对多的关系.需要使用中间表,中 ...

  8. Oracle学习笔记_04_多表查询

    一.概念: 1.多表连接有以下几种分法: (1)内连接           vs          外连接 (左.右.满) (2)等值连接        vs         不等值连接 (3)非自连 ...

  9. MySql 在cmd下的学习笔记 —— 有关多表查询的操作(内连接,外连接,交叉连接)

    mysql> create table test5( -> id int, ) -> )engine myisam charset utf8; Query OK, rows affe ...

随机推荐

  1. Kong(v1.0.2)代理参考

    介绍 在本文中,我们将通过详细解释Kong的路由功能和内部工作原理来介绍它的代理功能. Kong公开了几个接口,可以通过两个配置属性进行调整: proxy_listen,它定义了一个地址/端口列表,K ...

  2. GPRS骨干网逻辑结构

    从逻辑上来说,GPRS通过在GSM网络结构中增添SGSN和GGSN两个新的网络节点来实现.由于增加了这两个网络节点,需要命名新的接口.图1说明了GPRS逻辑体系结构.表1给出了GPRS体系结构中的接口 ...

  3. 亚马逊EC2服务器申请+NODE服务器部署+阿里云域名申请+SSL证书使用

    最近,由于项目需要,自己申请了一台亚马逊用于部署网站测试,在使用期间,发现网上没有一篇非常完整的文章讲解从服务器申请到域名解析,SSL证书申请的整个流程.所以自己总结一下,以供大家学习! 一.亚马逊E ...

  4. (整理) .NET IIS性能优化

    本文收集了部分性能优化的方式,缓存.压缩.线程池调整等等,仅供参考. 1 .NET 程序中的调整 程序Sqlhelper中使用缓存 使用JSON序列化器(Jil)取代Json.NET 2 .NET 程 ...

  5. shell脚本三——正则表达式

    shell函数:shell中允许将一组命令集合或语句形成一段可用代码,这些代码块称为shell函数.给这段代码起个名字称为函数名,后续可以直接调用该段代码. 格式:fun() { 命令 } Shell ...

  6. python对mysql进行简单操作

    python 连接MySQL数据库,进行简单操作 一.连接MySQL数据库,关闭连接 import pymysql db = pymysql.connect(host="xxx.xxx.x. ...

  7. uni-app 调用支付宝支付

    本文讲解  uni-app如何调用支付宝进行支付,服务端为 .net编写. 客户端:uni-app 编写  1.根据服务端生成的订单信息发起支付. 服务端:.net 编写 1.生成订单信息.2.接收支 ...

  8. V先生:信息流广告标题党必备的500个热词

    稍微没有全都偏,简直仅仅只永远, 已经曾经就竟然,将要立刻刚偶然, 渐渐终于决忽然,难道连续又再三, 也许必须很非常,最太十分更马上, 越极总挺常常再,屡次一定也不还. 你一定不知道.如何.最.咋.是 ...

  9. 新的blog站地址

    自己用Jekyll搭建了一个静态blog网站 以后的blog随笔就更新在:https:/blog.hudunsec.cn

  10. python学习(list增删改查、及常用方法)

    1.Python多条件判断: 多条件判断if: passelse: pass循环 while for i = 0 while i > 1: print('hello') else: print( ...