首页 > 数据库 >MySQL聚合函数

MySQL聚合函数

时间:2022-11-07 20:36:49浏览次数:56  
标签:set 聚合 函数 0.00 mysql sec MySQL +--------+ SELECT


AVG()                    平均值

COUNT()             计数

MAX()                   最大值

MIN()                    最小值

SUM()                  求和

先预备一张表,结构如下

mysql> DESC goodstest;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| id       | smallint(6)          | NO   | PRI | NULL    | auto_increment |
| goodname | varchar(20)          | NO   |     | NULL    |                |
| price    | smallint(5) unsigned | YES  |     | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

记录如下

mysql> SELECT * FROM goodstest;
+----+----------+-------+
| id | goodname | price |
+----+----------+-------+
|  1 | A        |     1 |
|  2 | B        |     2 |
|  3 | C        |     3 |
|  4 | D        |     4 |
|  5 | E        |     5 |
|  6 | F        |     6 |
|  7 | G        |     7 |
+----+----------+-------+
7 rows in set (0.00 sec)

例1:求平均值

mysql> SELECT AVG(id) FROM goodstest;
+---------+
| AVG(id) |
+---------+
|  4.0000 |
+---------+
1 row in set (0.00 sec)

例2:取出id排名前三的记录

mysql> SELECT * FROM goodstest LIMIT 3;
+----+----------+-------+
| id | goodname | price |
+----+----------+-------+
|  1 | A        |     1 |
|  2 | B        |     2 |
|  3 | C        |     3 |
+----+----------+-------+
3 rows in set (0.00 sec)

算出平均值并按照新的名字输出

mysql> SELECT AVG(price) AS ave_price FROM goodstest ;
+-----------+
| ave_price |
+-----------+
|    4.0000 |
+-----------+
1 row in set (0.00 sec)

例3:四舍五入

mysql> SELECT ROUND(2.4561,2);
+-----------------+
| ROUND(2.4561,2) |
+-----------------+
|            2.46 |
+-----------------+
1 row in set (0.00 sec)

mysql> SELECT ROUND(2.4561,3);
+-----------------+
| ROUND(2.4561,3) |
+-----------------+
|           2.456 |
+-----------------+
1 row in set (0.00 sec)

例4:COUNT 计数

mysql> SELECT COUNT(id) AS counts FROM goodstest;
+--------+
| counts |
+--------+
|      7 |
+--------+
1 row in set (0.00 sec)

例5:求最大值

mysql> SELECT MAX(price) AS counts FROM goodstest;
+--------+
| counts |
+--------+
|      7 |
+--------+
1 row in set (0.00 sec)

例6:求最小值

mysql> SELECT MIN(price) AS counts FROM goodstest;
+--------+
| counts |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)

例7:求总数

mysql> SELECT SUM(price) AS counts FROM goodstest;
+--------+
| counts |
+--------+
|     28 |
+--------+
1 row in set (0.00 sec)



标签:set,聚合,函数,0.00,mysql,sec,MySQL,+--------+,SELECT
From: https://blog.51cto.com/u_11320078/5831315

相关文章

  • MySQL信息函数
    CONNECTION_ID()              连接ID,返回ID号,也是线程的IDDATEBASE()                          当前数据库LAST_INSERT_ID()......
  • MySQL比较运算符和函数
    本文重点是三个比较运算符1,[NOT[BETWEEN...AND...2 , [NOT]IN()3 , IS[NOT]NULL例1:BETWEEN...AND..如果数字A在设定范围之内,返回TRUE,否则返回FAUSEmysql>SE......
  • MySQL数值运算符和函数
    数值运算符和函数CEIL()         进一取整DIV                 整数除法FLOOR()       舍一取整MOD              ......
  • MySQL自定义函数
    首先,做一个热身。引进一个系统函数LAST_INSERT_ID();这个函数的功能就是放回上一次插入的数据的id做个示范插入数据                       ......
  • MySQL日期时间函数
    日期时间函数NOW()                                           当前日期和时间CURDATE()                ......
  • C++溢出对象虚函数表指针
      C++一特性是通过virtual关键字实现运行时多态,虽然自己用到这个关键字的机会不多,但很多引用的第三方库会大量使用这个关键字,比如MFC...如果某个函数由virtual关键字修......
  • Go | 函数(包)的使用
    实现加减乘除运算传统方法实现:varn1float64=1.2varn2float64=2.1varoptbyte='+'varresfloat64switchopt{case'+':res=n1+n2ca......
  • 函数提高
    3函数提高3.1函数默认参数在C++中,函数的形参列表中的形参是可以有默认值的。语法:返回值类型函数名(参数=默认值){}示例:intfunc(inta,intb=10,intc=10)......
  • MySQL基础篇
    一.MySQL概述(一).什么是数据库?数据库就是存储数据的仓库,简称DB(database);SQL是操作数据库的编程语言,是一套标准,通过编写SQL语句来操作数据库;(二).关系型数据库是将数......
  • mysql 错误:The driver has not received any packets from the server.
    com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:CommunicationslinkfailureThelastpacketsentsuccessfullytotheserverwas0millisecondsago.The......