• Mysql的子查询与连接查询


    子查询:

    在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句

    主查询和子查询的关系:

    子查询是嵌入到主查询中,子查询是辅助主查询的,要么充当条件,要么充当数据源,子查询是可以独立存在的语句,是一条完整的 select 语句

    子查询分类:

    标量子查询: 子查询返回的结果是一个数据(一行一列)    一般使用的是比较运算符

    select * from student where age > (select avg(age) from student);

    列子查询: 返回的结果是一列(一列多行)    一般使用in

    select * from student where tid in (select id from teacher);

    行子查询: 返回的结果是一行(一行多列)          需求: 查找班级年龄最大,身高最高的学生

                  行元素: 将多个字段合成一个行元素,在行级子查询中会使用到行元素

    select * from student where (height,age) = (select max(height),max(age) from student);

    执行顺序为:

    from 表名

    where ....

    group by ...

    select distinct *

    having ...

    order by ...

    limit start,count

    实际使用中,只是语句中某些部分的组合,而不是全部

    连接查询:

    select 字段 from 表名1 inner join  表名2 on 关联条件                               

    内连接查询:查询的结果为两个表匹配到的数据

    select 字段 from 表名1 right join  表名2 on 关联条件

    右连接查询:查询的结果为两个表匹配到的数据,右表特有的数据,对于左表中不存在的数据使用null填充

    select 字段 from 表名1 left join  表名2 on 关联条件

    左连接查询:查询的结果为两个表匹配到的数据,左表特有的数据,对于右表中不存在的数据使用null填充

  • 相关阅读:
    SwiftUI Github App 源码 All In One
    React Design Patterns All In One
    centos6.9 install samba
    【Nginx】配置文件
    C#实现连接池
    LINUX安装nginx详细步骤
    C#之ref参数和out参数的区别
    【Nginx】反向代理、动静分离、负载均衡、Rewrite、防盗链
    python之requests库
    python \r和\n
  • 原文地址:https://www.cnblogs.com/dashenisme/p/10066798.html
Copyright © 2020-2023  润新知