聚合函数
count 返回查询结果的条数
max 返回查询结果的最大值
min 返回查询结果的最小值
sum 返回查询结果的和
avg 返回查询结果的平均值
 
统计分数大于等于90的人数:
mysql> select count(*) from new_student
        -> where score >="90"; 
 
 
使用distinct剔除字段值重复的条数
mysql> select count(distinct score) from new_student
        -> where score >="90";
 
统计最高分-max
mysql> select max(score) from new_student;
 
统计最低分-min
mysql> select min(score) from new_student;
 mysql> select min(score) from new_student
        -> where score >=60;
 
统计分数大于等于90的分数的和-sum
 mysql> select sum(score) from new_student
        -> where score >="90";
 
统计平均数-avg
 mysql> select avg(score) from new_student
        -> where score >="80";
 
分组查询
语法格式;
select [聚合函数] 字段名 from 表名
where 查询条件
group by 字段名
having 过滤条件
 
mysql> select score,count(*) from new_student
        -> where score >=80
        -> group by score;
 
mysql> select score,count(*) from new_student
        -> where score >=80             
        -> group by score
        -> having score >=90;
注:having子语句与where子语句区别:前者在分组后对记录进行过滤,后者在分组前对记录进行过滤
 
 
mysql> select score,count(*) from new_student

        -> where score >=80
        -> group by score
        -> having score >=90
        -> order by score desc;
 
 
 
联合查询
语法格式
select 语句
union [all]
select 语句
...
 注:联合查询结果使用第一个select语句中的字段名
 
mysql> select * from test_wl
        -> union
        -> select * from test_wu;
 
 
 

mysql基础(3)-高级查询的更多相关文章

  1. MySQL基础架构之查询语句执行流程

    这篇笔记主要记录mysql的基础架构,一条查询语句是如何执行的. 比如,在我们从student表中查询一个id=2的信息 select * from student where id=2; 在解释这条 ...

  2. 10月17日下午MySQl数据库CRUD高级查询

    高级查询:1.连接查询 #适用于有外键关系的  没有任何关系没法用select * from Info,Nation #同时查询这俩表并把两表每个数据相互组合,形成笛卡尔积 select * from ...

  3. Mysql 基本语句 + 高级查询

    MySQL执行SQL脚本文件的命令: 从cmd进入mysql命令行模式: mysql> -uroot –prootpassword –Ddatabasename 如果是我本地的数据库,就相应修改 ...

  4. mysql中的高级查询

    以前学习的查询语法: select 字段名 from 表名 where 条件 其实,查询的语法变化很多: 1. select 可以查询表达式, 表达式就是 运算符+操作数. 比如 1 + 1 2 * ...

  5. MySQL基础7-分页查询

    1.分页查询(MySQL特有的,oracle中没有) 栗子1: 每页最多3条记录:pageSize=3:第一页:SELECT * FROM product LIMIT 0,3第二页:SELECT * ...

  6. mysql中的高级查询语句

    此随笔用到的数据全是来自  关于mysql中表关系的一些理解(一对一,一对多,多对多) 提及的    学院表,学生表,学生详情表,选课表,课程表 单标查询:(查看学生表的学生名单) select st ...

  7. MySql数据and高级查询

    1.CREATE TABLE grade(stuID INT AUTO_INCREMENT PRIMARY KEY NOT NULL,stuName VARCHAR(32),stuAge INT ) ...

  8. MySQL基础6-分组查询

    1.分组函数 需求20:查询所有商品平均零售价SELECT AVG(salePrice) FROM product 需求21:查询商品总记录数SELECT COUNT(id) count FROM p ...

  9. MySQL基础4-SQL简单查询(单表)

    1.SELECT语句 2.运算符的优先级 利用Navicat中的查询方法: 栗子1:查询所有货品信息 栗子2:查询所有货品的id,productName,salePrice 当查询错误的时候出现的界面 ...

  10. MySQL基础语句(查询)

    students表 id class_id name gender score 1 1 小明 M 90 2 1 小红 F 95 3 1 小军 M 88 4 1 小米 F 73 5 2 小白 F 81 ...

随机推荐

  1. 学习 《UNIX网络编程》

    学习本书之前,为了了解C语言,先通读了<C程序设计语言>.但对C语言的理解.熟悉可能还是不足,所以在学习本书的过程中,遇到看不懂的C代码,还要去查询.思考. 本书一开始,我就遇到了问题,运 ...

  2. Cookies with curl the command line tool

    w https://curl.haxx.se/docs/http-cookies.html curl has a full cookie "engine" built in. If ...

  3. python系列四:Python3字符串

    #!/usr/bin/python #Python3 字符串#可以截取字符串的一部分并与其他字段拼接var1 = 'Hello World!'print ("已更新字符串 : ", ...

  4. What are DESC and ASC Keywords?

    What are DESC and ASC Keywords? ASC is the short form for ascending DESC is the short form for desce ...

  5. Python——用正则求时间差

    如有求时间差的需求,可直接套用此方法: import time true_time=time.mktime(time.strptime('2017-09-11 08:30:00','%Y-%m-%d ...

  6. python Flask框架CBV视图

    1.演示之前需要先写一个装饰器 # 装饰器 def wrapper(func): def inner(*args, **kwargs): print('操作函数%s之前' % func.__name_ ...

  7. android自定义控件(四)坐标系

    1.局部坐标系(Local Coordinate) 所谓本地坐标系,就是坐标系以物体的中心为坐标原点,物体旋转.平移等操作都是围绕局部坐标系进行的.这时当物体模型进行旋转.平移等操作时, 局部坐标系也 ...

  8. 017-Hadoop Hive sql语法详解7-去重排序、数据倾斜

    一.数据去重排序 1.1.去重 distinct与group by 尽量避免使用distinct进行排重,特别是大表操作,用group by代替 -- 不建议 select DISTINCT key ...

  9. Latex技巧:插入参考文献

    LaTeX插入参考文献,可以使用BibTex,也可以不使用BibTex. 方法一:不使用BibTeX 先在文章文章末尾写好需要插入的参考文献,逐一写出,例如: \begin{thebibliograp ...

  10. Win8.1和office2013使用电话激活步骤

    Win8.1和office2013使用电话激活步骤: 先从Win8.1贴吧的最后几个回复中找到几个密钥,最后的通常是最新的,然后输入到Win8.1中,会提示你密钥无效,如果有效你就走了狗屎运了,无效的 ...