MySQL数值及聚合函数

本文介绍了MySQL中常用的数值函数,包括abs()、sqrt()、mod()、rand()、round()和truncate(),并展示了它们的使用示例。此外,还详细解释了聚合函数max()、min()、sum()、avg()和count()的功能和应用。这些函数在数据库操作中对于数据处理和分析至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数值函数

abs()

取绝对值

mysql> select abs(1);
+--------+
| abs(1) |
+--------+
|      1 |
+--------+
1 row in set (0.00 sec)

mysql> select abs(-1);
+---------+
| abs(-1) |
+---------+
|       1 |
+---------+
1 row in set (0.01 sec)

aqrt()

求二次方根

注意

当为负数时,返回null

mysql> select sqrt(4);
+---------+
| sqrt(4) |
+---------+
|       2 |
+---------+
1 row in set (0.00 sec)

mysql> select sqrt(2);
+--------------------+
| sqrt(2)            |
+--------------------+
| 1.4142135623730951 |
+--------------------+
1 row in set (0.00 sec)

mysql> select sqrt(-2);
+----------+
| sqrt(-2) |
+----------+
|     NULL |
+----------+
1 row in set (0.00 sec)

mod()

取余

mysql> select mod(12,3);
+-----------+
| mod(12,3) |
+-----------+
|         0 |
+-----------+
1 row in set (0.00 sec)

mysql> select mod(13,4);
+-----------+
| mod(13,4) |
+-----------+
|         1 |
+-----------+
1 row in set (0.00 sec)

mysql> select mod(13,-4);
+------------+
| mod(13,-4) |
+------------+
|          1 |
+------------+
1 row in set (0.00 sec)

mysql> select mod(-13,-4);
+-------------+
| mod(-13,-4) |
+-------------+
|          -1 |
+-------------+
1 row in set (0.01 sec)

mysql> select mod(-13,4);
+------------+
| mod(-13,4) |
+------------+
|         -1 |
+------------+
1 row in set (0.00 sec)

rand()

生成一个0~1之间的随机数,传入整数参数是,用来产生重复序列

mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.6796255388107388 |
+--------------------+
1 row in set (0.00 sec)

mysql> select 10+rand();
+--------------------+
| 10+rand()          |
+--------------------+
| 10.386429689253148 |
+--------------------+
1 row in set (0.00 sec)

mysql> select (10+rand())*30;
+--------------------+
| (10+rand())*30     |
+--------------------+
| 326.79815581701524 |
+--------------------+
1 row in set (0.00 sec)

round(num,n)

四舍五入

注意

​ n表示要保留的小数点位数,不写默认为0

mysql> select round(1234.98765,3);
+---------------------+
| round(1234.98765,3) |
+---------------------+
|            1234.988 |
+---------------------+
1 row in set (0.00 sec)

mysql> select round(1234.98765);
+-------------------+
| round(1234.98765) |
+-------------------+
|              1235 |
+-------------------+
1 row in set (0.00 sec)

mysql> select round(1234.98765,7);
+---------------------+
| round(1234.98765,7) |
+---------------------+
|        1234.9876500 |
+---------------------+
1 row in set (0.00 sec)


truncate()

截取小数点后的位数,

注意

truncate()函数和round()函数不同,round()函数是四舍五入,而truncate()函数是直接保留小数点后指定的位数,当指定的位数为负数时,从小数点左边开始计算

mysql> select truncate(1134.34566,3);
+------------------------+
| truncate(1134.34566,3) |
+------------------------+
|               1134.345 |
+------------------------+
1 row in set (0.00 sec)

mysql> select truncate(1134.34566);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

mysql> select truncate(1134.34566,-1);
+-------------------------+
| truncate(1134.34566,-1) |
+-------------------------+
|                    1130 |
+-------------------------+
1 row in set (0.00 sec)

mysql> select truncate(1134.34566,-3);
+-------------------------+
| truncate(1134.34566,-3) |
+-------------------------+
|                    1000 |
+-------------------------+
1 row in set (0.00 sec)

pow()

pow(x,y)

求x的y次方根 等价于 power(x,y)

mysql> select pow(10,3);
+-----------+
| pow(10,3) |
+-----------+
|      1000 |
+-----------+
1 row in set (0.02 sec)

mysql> select pow(10,-3);
+------------+
| pow(10,-3) |
+------------+
|      0.001 |
+------------+
1 row in set (0.00 sec)

mysql> select pow(5,-2);
+-----------+
| pow(5,-2) |
+-----------+
|      0.04 |
+-----------+
1 row in set (0.00 sec)


聚合函数

max()

求最大值

mysql> select max(age) from text;
+----------+
| max(age) |
+----------+
|       89 |
+----------+
1 row in set (0.00 sec)

min()

求最小值

mysql> select min(age) from text;
+----------+
| min(age) |
+----------+
|       10 |
+----------+
1 row in set (0.00 sec)

sum()

求和

mysql> select sum(age) from text;
+----------+
| sum(age) |
+----------+
|      221 |
+----------+
1 row in set (0.00 sec)

avg()

求平均数

mysql> select avg(age) from text;
+----------+
| avg(age) |
+----------+
|  36.8333 |
+----------+
1 row in set (0.00 sec)

count()

求总数

mysql> select count(1) from text;
+----------+
| count(1) |
+----------+
|        6 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from text;
+----------+
| count(*) |
+----------+
|        6 |
+----------+
1 row in set (0.00 sec)

注意*

在没有主键的情况下,count(1)的执行效率必count(*)快

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值