zoukankan      html  css  js  c++  java
  • mysql_DML_select_聚合join

    聚合函数:

    select avg(salary)//平均值

    from wsb;

    select sum(salary)//总和

    from wsb;

    select max(salary)//最大

    from wsb;

    select min(salary)// 最小

    from wsb;

    select count(*)// 统计多少数据   配合where salary>500;

    from wsb;

    分组:分别计算  group by

    select sex,count(*) 

    from wsb2 group by sex; group by后面不能有where只能用having

    where后面不能跟聚合函数having可以

    eg1:

    select a.Name,SUM(b.Grade)
    from students a ,Score b
    GROUP BY a.Id,b.Stu_id, a.Name
    HAVING SUM(b.Grade)>100;

    eg:

    select a.Name,SUM(b.Grade)
    from students a ,Score b
    GROUP BY a.Id,b.Stu_id
    HAVING a.Id=b.Stu_id;  having如果后面没有跟聚合函数 那么对应字段必须在group by中有相应字段 

    join:
    Left join:左连接, 连接两张表,以左边表的数据匹配右边表中的数据,如果左边
    表中的数据在右边表中没有,会显示左边表中的数据。
    Right join:右连接,连接两张表,以右边表的数据匹配左边表中的数据,如果左边
    表中的数据在左边边表中没有,会显示右边表中的数据。
    Inner join:内连接,连接两张表,匹配两张表中的数据,和前面两个不同的是只会
    显示匹配的数据。
    select a.name 学生姓名,b.score 学生成绩 from students a left join score b on
    a.id=b.student_id;
    select a.name 学生姓名,b.score 学生成绩 from students a right join score b on
    a.id=b.student_id;
    select a.name 学生姓名,b.score 学生成绩 from students a INNER join score b on
    a.id=b.student_id;
    select a.name 学生姓名,b.score 学生成绩 from students a,score b where
    a.id=b.student_id;
  • 相关阅读:
    HTTP协议简介
    Hadoop日记Day1---Hadoop介绍
    Linux日记Day3---Linux的文件属性与目录配置
    Hadoop日记Day4---去除HADOOP_HOME is deprecated
    Android 系统名字、版本、API level的对应关系
    win10搭建selendroid测试环境
    初识selendroid
    学习selendroid初衷
    Contos 安装nodeJs环境
    Failed to write HTTP message,Could not write JSON错误
  • 原文地址:https://www.cnblogs.com/lingxia/p/5897229.html
Copyright © 2011-2022 走看看