mysql多表查询

ning23 / 2023-08-03 / 原文

mysql多表查询

1.内连接查询

  1. 隐式内连接查询

    select * from table1,table2 where conditions;
    

    注:如果起别名,条件和查询的内容需要用别名来调用

  2. 显示内连接查询

    select * from table1 [inner] join table2 on conditions;
    

2.外连接查询

  1. ①左表外连接(左表中的全部数据)

    •   select 字段列表 from table1 left [outer] join table2 on conditions;
      

      包括左表中的所有数据和右表中的对应数据

  2. ②右表外连接(右表中的全部数据)

    •   select 字段列表 from table1 right [outer] join table2 on conditions;
      

      包括右表中的所有数据和左表中的对应数据

3.子查询

mysql中的嵌套select语句,称为嵌套查询,又称为子查询

select 字段名称 from table1 where 条件1=(select 字段名称 from table2 where conditions);
  1. 标量子查询

    子查询的返回结果是单个值

    常用操作符 = < > <= >= <>

  2. 列子查询

    自查询的返回结构是一行(可以是多列)

    常用操作符 in、not in 等

  3. 行子查询

    子查询的返回结果是一列(可以是多行)

    常见操作符 = 、<>、 in 、not in

  4. 表子查询

    子查询的返回结果是多行多列

    常见的操作符 in

    这个多行多列的表可以作为一个子表来使用

    select * from 子表[as] table2,table1 where conditions;