美文网首页mysql
2020-08-13多表查询

2020-08-13多表查询

作者: 智障猿 | 来源:发表于2020-08-22 09:53 被阅读0次

    多表查询

    • 语法
      select 列名列表 from 表名列表 where
    • 笛卡尔积
      有两个集合A,B,取这两个集合的所有组成情况
      要完成多表查询,需要消除无用的数据
    • 多表查询消除无用数据的分类
    1. 内连接查询
      ①隐式内连接
      select * from 表1,表2 where 表名.列名1= 表名.列名2;
      select * from 表1 t1,表2 t2 where t1.列名 = t2.列名;
      ②显示内连接
      语法:select 字段列表 from 表名1 inner join 表名2 on 条件;
      演示
      select * from student 【inner】 join class on student.cid = class.id;
      select * from student join class on student.cid = class.id;
      注意
      ①从哪些表中查询数据
      ②条件是什么
      ③查询哪些字段

    2. 外连接查询
      ①左外连接:查询的是左表所有数据以及交集部分。
      select 字段列表 from 表1 left 【outer】 join 表2 on 条件;
      ②右外连接:查询的是右表所有数据及交集部分。
      select 字段列表 from 表1 right 【outer】join 表2 on 条件;

    3. 子查询:查询中嵌套查询,嵌套的查询是子查询
      select * from 表名 where 列名=(select MAX(列名) from 表名);

    子查询

    • 子查询的结果是单行单列的:
      子查询作为条件,使用运算符与之进行比较:>,<,<>,=,
      select * from student where 列名 >(select AVG(列名) from result );
    • 子查询的结果是多行单列的:
      子查询作为条件,使用运算符in进行判断
      select * from studen where 列名 in (select 列名 from where 列名=值 OR 列名=值);
    • 子查询的结果是多行多列的:
      子查询可以作为一张虚拟表
      select * from 表1 t1,(select * from 表2 where 列名 > 值)t2 where t1.列名 = t.列名

    相关文章

      网友评论

        本文标题:2020-08-13多表查询

        本文链接:https://www.haomeiwen.com/subject/gxlbdktx.html