一、MySQL常用的数学函数
常用的数学函数和用法
函数名 |
注释 |
ads(x) |
输出x的绝对值 |
rand() |
输出0到1之间的一个随机数 [0~1),可以返回0但是不能返回1 |
mod(x,y) |
输出x对y取余的值 |
power(x,y) |
输出x的y次方的值 |
round(x) |
输出离x最近的整数 |
round(x,y) |
输出保留x的y位的小数四舍五入后的值 |
sqrt(x) |
输出x的平方根的值 |
truncate(x,y) |
输出数字x截断位为y位小数的值 |
ceil(x) |
返回大于或等于x的最小整数 |
floor(x) |
返回小于或等于x的最大整数 |
greatest(x,y,z,…) |
返回集合中最大的值 |
least(x,y,z,…) |
返回集合中最小的值 |
用法实例
abs()用法:
mysql> select abs(5-7),abs(-9),abs(-1.34);
+----------+---------+------------+
| abs(5-7) | abs(-9) | abs(-1.34) |
+----------+---------+------------+
| 2 | 9 | 1.34 |
+----------+---------+------------+
rand()的用法:
mysql> select rand(),rand()*10,rand()*100;
+---------------------+--------------------+--------------------+
| rand() | rand()*10 | rand()*100 |
+---------------------+--------------------+--------------------+
| 0.21567710974782436 | 0.4465255424813605 | 57.623147273085216 |
+---------------------+--------------------+--------------------+
mod(x,y)的用法:
mysql> select mod(55,10),mod(38,5),mod(69,5);
+------------+-----------+-----------+
| mod(55,10) | mod(38,5) | mod(69,5) |
+------------+-----------+-----------+
| 5 | 3 | 4 |
+------------+-----------+-----------+
power(x,y)的用法:
mysql> select power(2,5),power(-2,5),power(1.23,5);
+------------+-------------+--------------------+
| power(2,5) | power(-2,5) | power(1.23,5) |
+------------+-------------+--------------------+
| 32 | -32 | 2.8153056842999997 |
+------------+-------------+--------------------+
round(x)、round(x,y)的用法:
mysql> select round(2.7),round(2.567,2),round(2.3567),round(2.898,1);
+------------+----------------+---------------+----------------+
| round(2.7) | round(2.567,2) | round(2.3567) | round(2.898,1) |
+------------+----------------+---------------+----------------+
| 3 | 2.57 | 2 | 2.9 |
+------------+----------------+---------------+----------------+
sqrt(x)的用法:
mysql> select sqrt(9),sqrt(-4),sqrt(1.34);
+---------+----------+--------------------+
| sqrt(9) | sqrt(-4) | sqrt(1.34) |
+---------+----------+--------------------+
| 3 | NULL | 1.1575836902790226 |
+---------+----------+--------------------+
truncate(x,y)|的用法:
mysql> select truncate(1.234,2),truncate(2,3),truncate(-1.345,2);
+-------------------+---------------+--------------------+
| truncate(1.234,2) | truncate(2,3) | truncate(-1.345,2) |
+-------------------+---------------+--------------------+
| 1.23 | 2 | -1.34 |
+-------------------+---------------+--------------------+
ceil(x)、floor(x)的用法:
mysql> select ceil(1.3),floor(1.5);
+-----------+------------+
| ceil(1.3) | floor(1.5) |
+-----------+------------+
| 2 | 1 |
+-----------+------------+
greatest(x,y,z,...)、least(x,y,z,...)的用法:
mysql> select greatest(1,2,3),least(1,2,3);
+-----------------+--------------+
| greatest(1,2,3) | least(1,2,3) |
+-----------------+--------------+
| 3 | 1 |
+-----------------+--------------+
例:
随机生成一个1~100的值
mysql> select ceil(rand()*100),ceil(rand()*100),ceil(rand()*100);
+------------------+------------------+------------------+
| ceil(rand()*100) | ceil(rand()*100) | ceil(rand()*100) |
+------------------+------------------+------------------+
| 73 | 3 | 98 |
+------------------+------------------+------------------+
随机生成0到5的值
mysql> select mod(ceil(rand()*100),6),mod(ceil(rand()*100),6),mod(ceil(rand()*100),6);
+-------------------------+-------------------------+-------------------------+
| mod(ceil(rand()*100),6) | mod(ceil(rand()*100),6) | mod(ceil(rand()*100),6) |
+-------------------------+-------------------------+-------------------------+
| 1 | 0 | 5 |
+-------------------------+-------------------------+-------------------------+
二、常用的聚合函数
常用的聚合函数和用法
聚合函数是对表中数据记录进行集中概括而设计的一类函数
函数名 |
注释 |
avg() |
输出指定列的平均值 |
count() |
输出指定列统计到的非NULL的值的个数 |
min() |
输出指定列的最小值 |
max() |
输出指定列的最大值 |
sum() |
输出指定列的所有值之和 |
用法实例
查看数据库stu表中的记录
mysql> select * from stu;
+----+---------+-------+------+------+
| id | name | score | hoby | dizi |
+----+---------+-------+------+------+
| 1 | tianxia | 78.00 | 2 | nj |
| 2 | diyi | 89.00 | 1 | nj |
| 3 | wode | 55.00 | 1 | NULL |
| 4 | tade | 57.00 | 3 | NULL |
| 5 | nida | 66.00 | 1 | nj |
| 6 | liushou | 55.00 | 3 | |
| 7 | xiaoer | 69.00 | 10 | bj |
| 8 | dage | 78.00 | 8 | tj |
+----+---------+-------+------+------+
使用avg()函数输出stu表中score字段的平均值:
mysql> select avg(score) from stu;
+------------+
| avg(score) |
+------------+
| 68.375000 |
+------------+
使用count()函数输出stu表中dizi字段的非NULL的记录值:
mysql> select count(dizi) from stu;
+-------------+
| count(dizi) |
+-------------+
| 6 |
+-------------+
注:对比stu表可以发现,count()函数可以统计为空值但是不会统计为NULL的值
使用min()和max()函数输出stu表中score字段的最小值和最大值:
mysql> select min(score),max(score) from stu;
+------------+------------+
| min(score) | max(score) |
+------------+------------+
| 55.00 | 89.00 |
+------------+------------+
使用sum()函数输出stu表中score字段的所有记录和的值:
mysql> select sum(score) from stu;
+------------+
| sum(score) |
+------------+
| 547.00 |
+------------+
三、常用的字符串函数
常用字符串函数的用法
函数名 |
注释 |
length(x) |
返回字符串x的长度 |
trim() |
返回去除头和尾部的空格字符,但是不能去除字符间的空格 |
concat(x,y) |
将提供的参数x和y拼接成一个字符串 |
upper(x) |
将字符串x的所有字母变成大写字母 |
lower(x) |
将字符串x的所有字母变成小写字母 |
left(x,y) |
返回字符串x的前y个字符 |
right(x,y) |
返回字符串x的后y个字符 |
repeat(x,y) |
将字符串x重复y次 |
space(x) |
返回x个空格 |
replace(x,y,z) |
将字符串z替代字符串x中的字符串y |
strcmp(x,y) |
比较x和y,返回的值可以为-1,0,1 |
substring(x,y,z) |
获取从字符串x中的第y个位置开始长度为z的字符串 |
reverse(x) |
将字符串x反转 |
用法实例
length()函数的用法:
mysql> select length('qerq'),length(''),length('NULL');
+----------------+------------+----------------+
| length('qerq') | length('') | length('NULL') |
+----------------+------------+----------------+
| 4 | 0 | 4 |
+----------------+------------+----------------+
trim()函数的用法:
mysql> select trim(' wer '),trim(' w e r '),trim(' ti an');
+-----------------+-----------------+-----------------+
| trim(' wer ') | trim(' w e r ') | trim(' ti an') |
+-----------------+-----------------+-----------------+
| wer | w e r | ti an |
+-----------------+-----------------+-----------------+
concat()、upper()、lower()函数的用法:
mysql> select concat('wer','e34t'),upper('AB3ew'),lower('we5rER');
+----------------------+----------------+-----------------+
| concat('wer','e34t') | upper('AB3ew') | lower('we5rER') |
+----------------------+----------------+-----------------+
| were34t | AB3EW | we5rer |
+----------------------+----------------+-----------------+
left(),right(),repeat()函数的用法:
mysql> select left('sdaff',3),right('werdds',3),repeat('sfd',2);
+-----------------+-------------------+-----------------+
| left('sdaff',3) | right('werdds',3) | repeat('sfd',2) |
+-----------------+-------------------+-----------------+
| sda | dds | sfdsfd |
+-----------------+-------------------+-----------------+
concat()、replace()函数的用法:
mysql> select concat('we',space(2),'are'),replace('werer','er',34);
+-----------------------------+--------------------------+
| concat('we',space(2),'are') | replace('werer','er',34) |
+-----------------------------+--------------------------+
| we are | w3434 |
+-----------------------------+--------------------------+
strcmp()函数的用法:
mysql> select strcmp('a','b'),strcmp('fsd','er'),strcmp('af2','af2');
+-----------------+--------------------+---------------------+
| strcmp('a','b') | strcmp('fsd','er') | strcmp('af2','af2') |
+-----------------+--------------------+---------------------+
| -1 | 1 | 0 |
+-----------------+--------------------+---------------------+
substring(),reverse()函数的用法:
mysql> select substring('ersar',2,3),reverse('sd34sdf');
+------------------------+--------------------+
| substring('ersar',2,3) | reverse('sd34sdf') |
+------------------------+--------------------+
| rsa | fds43ds |
+------------------------+--------------------+
四、常用的时间函数
常用的时间函数用法
函数名 |
注释 |
curdate() |
返回当前时间的年月日 |
curtime() |
返回当前时间的时分秒 |
now() |
返回当前时间的日期和时间 |
month(x) |
返回日期×中的月份值 |
week(x) |
返回日期×是年度第几个星期 |
hour(x) |
返回×中的小时值 |
minute(x) |
返回×中的分钟值 |
second(x) |
返回×中的秒钟值 |
dayofweek(x) |
返回×是星期几,1星期日,2星期一 |
dayofmonth(x) |
计算日期×是本月的第几天 |
dayofyear(x) |
计算日期×是本年的第几天 |
用法实例
curdate()、curtime()、now()函数的用法:
mysql> select curdate(),curtime(),now();
+------------+-----------+---------------------+
| curdate() | curtime() | now() |
+------------+-----------+---------------------+
| 2020-10-18 | 01:01:02 | 2020-10-18 01:01:02 |
+------------+-----------+---------------------+
month()、week()函数的用法:
mysql> select month('2020-10-18'),week('2020-10-18');
+---------------------+--------------------+
| month('2020-10-18') | week('2020-10-18') |
+---------------------+--------------------+
| 10 | 42 |
+---------------------+--------------------+
hour()、minute()、second()函数的用法:
mysql> select hour('01:01:02'),minute('23:15:55'),second('15:26:25');
+------------------+--------------------+--------------------+
| hour('01:01:02') | minute('23:15:55') | second('15:26:25') |
+------------------+--------------------+--------------------+
| 1 | 15 | 25 |
+------------------+--------------------+--------------------+
dayofweek()、dayofmonth()、dayofyear()函数的用法:
mysql> select dayofweek('2020-10-18'),dayofmonth('2020-10-18'),dayofyear('2020-10-18');
+-------------------------+--------------------------+-------------------------+
| dayofweek('2020-10-18') | dayofmonth('2020-10-18') | dayofyear('2020-10-18') |
+-------------------------+--------------------------+-------------------------+
| 1 | 18 | 292 |
+-------------------------+--------------------------+-------------------------+