mysql 聚集函数和分组
1、sc表的内容如下:
mysql> select * from sc order by sid asc;
+----+-------+-----+-------+
| ID | SID | CID | SCORE |
+----+-------+-----+-------+
| 1 | 10001 | 101 | 70 |
| 2 | 10001 | 102 | 80 |
| 4 | 10001 | 103 | 90 |
| 3 | 10008 | 103 | 50 |
| 5 | 10008 | 101 | 60 |
| 6 | 10008 | 102 | 70 |
+----+-------+-----+-------+
6 rows in set
2、求所有成绩的平均值
mysql> select avg(score) from sc;
+------------+
| avg(score) |
+------------+
| 70.0000 |
+------------+
1 row in set
考虑,select sid,cid,avg(score) from sc;的结果是什么?
这个时候平均值只有一个,而sid,cid都是有多个,这种情况,sid,cid取第一条记录的值,sid,cid的值也没有意义。如果某一列所有的值都相同,才有意义。
3、求每一个学生的平均值
mysql> select sid,cid,avg(score) from sc group by sid;
+-------+-----+------------+
| sid | cid | avg(score) |
+-------+-----+------------+
| 10001 | 101 | 80.0000 |
| 10008 | 103 | 60.0000 |
+-------+-----+------------+
2 rows in set
这个时候,sid的值是有意义的,而cid的值没有意义。
mysql 聚集函数和分组的更多相关文章
- mysql 聚集函数 count 使用详解
mysql 聚集函数 count 使用详解 本文将探讨以下问题 1.count(*) . count(n).count(null)与count(fieldName) 2.distinct 与 coun ...
- mysql 聚集函数需要注意的问题
1.当没有记录的时候,使用聚集函数,会导致出现一条记录,记录的取值都是NULL,如下:mysql> select name from student where name='David';Emp ...
- mysql 聚集函数 count 使用详解(转载)
本文将探讨以下问题 1.count(*) . count(n).count(null)与count(fieldName)2.distinct 与 count 连用3.group by (多个字段) 与 ...
- SQL必知必会 -------- 聚集函数、分组排序
聚集函数 1.AVG()函数 输入:SELECT AVG(prod_price) AS avg_price FROM Products 输出: 警告:只用于单个列AVG()只能用来确定特定数值列的平均 ...
- mysql聚合函数和分组
文章实例的数据表,来自上一篇博客<mysql简单查询>:http://blog.csdn.net/zuiwuyuan/article/details/39349611 一. 聚合函数 聚合 ...
- mysql 字符串函数、分组函数
字符串函数 1.concat 函数 drop table test;create table test(id int(4), name varchar(10), sex char(2));insert ...
- MYSQL常用函数以及分组操作
SELECT CONVERT(",SIGNED); SELECT CAST(" AS SIGNED); SELECT ; SELECT LENGTH("姜浩真帅!&quo ...
- MySQL聚合函数与数据分组
我们最常需要的是汇总数据而不是把他们实际检索出来 确定表中行数(或满足某个条件或包含某个特定值的行数) 确定表中行组的和 找出表列(或所有行或特定列)的最大值,最小值和平均值 聚集函数是运行在行组上, ...
- Mysql-学习笔记(==》集合函数与分组四)
-- 聚集函数 配合分组语句 group by-- 显示最高分SELECT MAX(sscore) FROM db.`student`;-- 显示最高分学生的信息min maxSELECT * FRO ...
随机推荐
- LeetCode----67. Add Binary(java)
package addBinary67;/* Given two binary strings, return their sum (also a binary string).For example ...
- ASp.net 注册
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs ...
- 20150612_Andriod contextual action mode 菜单
参考地址:http://www.xuebuyuan.com/1114028.html http://www.cnblogs.com/mengdd/p/3564782.html ...
- drawer principle in Combinatorics
Problem 1: Given an array of real number with length (n2 + 1) A: a1, a2, ... , an2+1. Prove that th ...
- C#相对路径转绝对路径,绝对路径转相对路径
1.绝对路径转相对路径 绝对转相对似乎C#没有提供实现,需要自己写,这里摘选了一位博友的实现方法: string RelativePath(string absolutePath, string re ...
- interrupt ,interrupted 和 isInterrupted
1.interrupt interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位,不会停止线程.需要用户自己去监 ...
- 切分vocab时遇到的问题
vocab的格式如下所示,每个词和对应100维的向量: </s> 0.004003 0.004419 -0.003830 -0.003278 0.001367 0.003021 0.000 ...
- jquery简单插件到复杂插件(1)--tabs
写在前面,到了新公司开始转做前段,之前一直写php,一共写了半年,转过来,jq都用不好,但是还是得不断的学习,谁没菜过.从最简单的开始写,最近也在学习些html5的小游戏,加油吧.js原生写的可以说惨 ...
- 解决maven项目将model version改成3.0版本问题
找到项目目录,找到.setting文件 找到org.eclipse.wst.common.project.facet.core.xml文件 修改如下标签 <installed facet=&qu ...
- 集合、ArrayList 集合。Stack集合。Queue集合。以及Hashtable集合
arrayList 首先复制Colections加 : 创建arrayList ar =new arrayList(); //ArrayList al=new ArrayList(); ...