• mysql --子查询


     标量子查询

    把价格最高的商品取出来???

    select * from goods order by goods_price desc limit 1;

     可是价格最高的可能有很多条数据呢?

    所以,换一种方式

    1,查出最高的价格

    select max(goods_price) from goods;

    2,根据最高价查询

    select * from goods where goods_price = (select max(goods_price) from goods);

    列子查询

    1,查询一列

    select brand_id from brand;

    2,select * from goods where brand_id in (select brand_id from brand);

    行子查询

    我要查出价格最高而且brand_id最大的商品

    1,得到最高价,得到最大brand_id

    select max(goods_price),max(brand_id) from goods;

     

     2,根据条件查询

    select * from goods where (goods_price, brand_id)=(select max(goods_price),max(brand_id) from goods);

    表子查询

    查出每个品牌(brand_id)最低价的一个

    1,可以使用统计函数

      select goods_name,min(goods_price) from goods group by brand_id;

     2,使用表子查询,,不使用任何统计函数

      2.1,先查出每个品牌的最低价

      select * from goods order by goods_price asc;

      2.2,跟聚这个表再分组

      select * from (select * from goods order by goods_price asc) as g  group by  brand_id;

     有点问题, 先略过这里---------------------------------------------------------------------------

    3,使用自然内连接

    1,查出每个品牌的最低价

     select  brand_id,min(goods_price) as goods_price from goods group by brand_id;

     

     select * from goods natural join (select  brand_id,min(goods_price) as goods_price from goods group by brand_id)  as b;

  • 相关阅读:
    linux系统的nobody用户
    java包命名规则
    配置文件解析
    jps参数
    Java数据类型总结
    JSON与JAVA数据的相互转换
    maven中使用net.sf.json-lib
    设计 REST 风格的 MVC 框架
    Java 5种字符串拼接方式性能比较
    spring获取webapplicationcontext,applicationcontext几种方法详解
  • 原文地址:https://www.cnblogs.com/zhang19950924/p/13218264.html
Copyright © 2020-2023  润新知