zoukankan      html  css  js  c++  java
  • Mysql子查询

    1.知识结构图

    1.1 完整的子查询形式

    1.2 子查询应用位置

    1.3 子查询结果类型

    2.where型子查询

    (把内层查询结果当作外层查询的比较条件

    #不用order by 来查询最新的商品

    select goods_id,goods_name from goods where goods_id = (select max(goods_id) from goods);

    #取出每个栏目下最新的产品(goods_id唯一)

    select cat_id,goods_id,goods_name from goods where goods_id in(select max(goods_id) from goods group by cat_id); 

    3.from型子查询

    (把内层的查询结果外层再次查询)
    #用子查询查出挂科两门及以上的同学的平均成绩
    思路:
    #先查出哪些同学挂科两门以上

    select name,count(*) as gk from stu where score < 60 having gk >=2;

    #以上查询结果,我们只要名字就可以了,所以再取一次名字

    select name from (select name,count(*) as gk from stu having gk >=2) as t;

    #找出这些同学了,那么再计算他们的平均分

    select name,avg(score) from stu where name in (select name from (select name,count(*) as gk from stu having gk >=2) as t) group by name;

    4.exists型子查询

    (把外层查询结果拿到内层,看内层的查询是否成立)
    #查询哪些栏目下有商品,栏目表category,商品表goods

    select cat_id,cat_name from category where exists(select * from goods where goods.cat_id = category.cat_id);
  • 相关阅读:
    Windows下的符号链接
    简单谈谈存储器的容量缩水问题
    配置CKEditor和CKFinder
    CKFinder的水印控件的问题
    关于 Visual Studio 默认创建的不是公共类
    Symbian 60 按键以及对应的键值(图)
    PLC中存储器的数据类型与寻址方式
    S7200 寻址
    电工识图笔记
    S7200型号串口通信参数设置
  • 原文地址:https://www.cnblogs.com/blueoverflow/p/4713747.html
Copyright © 2011-2022 走看看