mysql

2020-08-13多表查询

2020-08-22  本文已影响0人  智障猿

多表查询

  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 表名);

子查询

上一篇 下一篇

猜你喜欢

热点阅读