• 子查询概念和分类


    • 创建表

    CREATE TABLE tb_grade(

      id INT(10) NOT NULL AUTO_INCREAMENT PRIMARY KEY,

     s_name VARCHAR(20) DEFAULT NULL,

    score int(3) DEFAULT 0,

    c_name VARCHAR(20) DEFAULT NULL

    ) comment ‘学生成绩表’

    • 初始化数据

    INSERT INTO tb_grade (s_name,score,c_name) VALUES

    (“刘一”,89,”一班”),

    (“陈二”,99,”二班”),

    (“张三”,69,”四班”);

    • 查询出成绩高于平均分的学生信息

    非相关子查询:独立于外部查询,子查询只执行一次,执行完将结果传递给外部查询

    1) select avg(score) from tb_grade;  --学生平均分

    2) select * from tb_grade where score >( select avg(score) from tb_grade );

    查询出成绩高于本班级成绩平均分的学生

    相关子查询:依赖于外部查询的数据,外部查询每执行一次,子查询就执行一次

    3) Eg: select * from tb_grade where c_name=’二班’ AND

     where score >( select avg(score) from tb_grade where c_name =’二班’);

    4)   select * from tb_grade t1

     where t1.score >( select avg(t2.score) from tb_grade t2

    where t2.c_name =t1.c_name);

    1. 查询出id最大的学生信息

    Select * from tb_grade order by id desc limit1;

    Select * from tb_grade where id=最大,使用子查询

    Select * from tb_grade where id=(select max(id) from tb_grade);

  • 相关阅读:
    Asp.net routing vs Url rewriting
    How to combine WCF Route and MVC Route to work together.
    Servlets beat CGI
    What if you encounter a problem when consume your WCF service ? How to Diagnostic it ?
    uva 4965 Sum the Square
    zoj 3633 Alice's present
    4966 Normalized Form
    ZOJ 3015 Collision Ball Game
    二分图 最小路径覆盖
    uva 2696 Air Raid
  • 原文地址:https://www.cnblogs.com/JacquelineQA/p/12539581.html
Copyright © 2020-2023  润新知