mysql学习笔记--数据库多表查询

一、内连接【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;