一. 常用的函数分类
• 数学函数
• 聚合函数
• 字符串函数
• 日期时间函数
二. 常用的数学函数
1.rand() 返回0到1的随机数
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.2819034413266028 |
+--------------------+
1 row in set (0.00 sec)
mysql> select mod(7,3);
+----------+
| mod(7,3) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
3.round(x) 返回离x最近的整数
mysql> select round(1.8);
+------------+
| round(1.8) |
+------------+
| 2 |
+------------+
1 row in set (0.00 sec)
4.round(x,y) 保留x的y位小数四舍五入后的值:
mysql> select round(1.78,1);
+---------------+
| round(1.78,1) |
+---------------+
| 1.8 |
+---------------+
1 row in set (0.00 sec)
5.truncate(x,y) 返回数字x截断为y位小数的值
mysql> select truncate(3.12345,3);
+---------------------+
| truncate(3.12345,3) |
+---------------------+
| 3.123 |
+---------------------+
1 row in set (0.00 sec)
6.ceil(x) 返回大于或等于x的最小整数
mysql> select ceil(1.09); // 向下取整
+------------+
| ceil(1.09) |
+------------+
| 2 |
+------------+
1 row in set (0.00 sec)
7.floor(x) 返回小于或等于x的最大整数
mysql> select floor(1.08);
+-------------+
| floor(1.08) |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
三.聚合函数
mysql> select avg(id) as '平均值' from xc_user;
+--------------------+
| 平均值 |
+--------------------+
| 49.333333333333336 |
+--------------------+
1 row in set (0.00 sec)
2.count() 返回指定列中非NULL值的个数
mysql> select count(id) as '非null个数' from xc_user;
+------------+
| 非null个数 |
+------------+
| 6 |
+------------+
1 row in set (0.00 sec)
mysql> select min(id) '最小值', max(id) '最大值' from xc_user;
+--------+--------+
| 最小值 | 最大值 |
+--------+--------+
| 46 | 52 |
+--------+--------+
1 row in set (0.00 sec)
4.sum() 返回指定列的所有值之和
mysql> select sum(id) '和' from xc_user;
+------+
| 和 |
+------+
| 296 |
+------+
1 row in set (0.00 sec)
四.常用的字符串函数
mysql> select length('abjcoiyuit');
+----------------------+
| length('abjcoiyuit') |
+----------------------+
| 10 |
+----------------------+
1 row in set (0.00 sec)
2.trim() 返回去除指定格式的值(只能去除前后字符的空格,中间的不行)
mysql> select trim(' gh ty ');
+--------------------+
| trim(' gh ty ') |
+--------------------+
| gh ty |
+--------------------+
1 row in set (0.00 sec)
4.concat(x,y) 将提供的参数x和y拼接成一个字符串
mysql> select concat('x','hfiwo');
+---------------------+
| concat('x','hfiwo') |
+---------------------+
| xhfiwo |
+---------------------+
1 row in set (0.00 sec)
5.upper(x) 将字符串x的所有字母变成大写字母;lower(x) 将字符串x的所有字母变成小写字母
mysql> select upper('anf'), lower('AHIBD');
+--------------+----------------+
| upper('anf') | lower('AHIBD') |
+--------------+----------------+
| ANF | ahibd |
+--------------+----------------+
1 row in set (0.00 sec)
6.left(x,y) 返回字符串x的前y个字符,right(x,y) 返回字符串x的后y个字符
mysql> select left('feiofghwv',3), right('feiowv',2);
+---------------------+-------------------+
| left('feiofghwv',3) | right('feiowv',2) |
+---------------------+-------------------+
| fei | wv |
+---------------------+-------------------+
1 row in set (0.00 sec)
7.replace(x,y,z) 将字符串z替代字符串x中的字符串y
mysql> select replace('tress','ss','aa');
+----------------------------+
| replace('tress','ss','aa') |
+----------------------------+
| treaa |
+----------------------------+
1 row in set (0.00 sec)
8.strcmp(x,y) 比较x和y,返回的值可以为-1,0,1
mysql> select strcmp(67,98);
+---------------+
| strcmp(67,98) |
+---------------+
| -1 |
+---------------+
1 row in set (0.00 sec)
mysql> select strcmp(2,1);
+-------------+
| strcmp(2,1) |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
mysql> select strcmp(3,3);
+-------------+
| strcmp(3,3) |
+-------------+
| 0 |
+-------------+
1 row in set (0.00 sec)
9.substring(x,y,z) 获取从字符串x中的第y个位置开始长度为z的字符串
mysql> select substring('mysqldiwnwknfs',3,4);
+---------------------------------+
| substring('mysqldiwnwknfs',3,4) |
+---------------------------------+
| sqld |
+---------------------------------+
1 row in set (0.00 sec)
10.reverse(x) 将字符串x反转
mysql> select reverse('adciwo');
+-------------------+
| reverse('adciwo') |
+-------------------+
| owicda |
+-------------------+
1 row in set (0.00 sec)
五.日期时间函数
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2020-09-13 |
+------------+
1 row in set (0.00 sec)
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 00:51:01 |
+-----------+
1 row in set (0.00 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2020-09-13 00:51:26 |
+---------------------+
1 row in set (0.00 sec)
mysql> select month('2020-6-26');
+--------------------+
| month('2020-6-26') |
+--------------------+
| 6 |
+--------------------+
1 row in set (0.00 sec)
mysql> select week('2020-6-26');
+-------------------+
| week('2020-6-26') |
+-------------------+
| 25 |
+-------------------+
1 row in set (0.04 sec)
mysql> select hour(curtime());
+-----------------+
| hour(curtime()) |
+-----------------+
| 0 |
+-----------------+
1 row in set (0.00 sec)
mysql> select minute(curtime());
+-------------------+
| minute(curtime()) |
+-------------------+
| 54 |
+-------------------+
1 row in set (0.00 sec)
mysql> select second(curtime());
+-------------------+
| second(curtime()) |
+-------------------+
| 29 |
+-------------------+
1 row in set (0.00 sec)
mysql> select dayofweek(curdate());
+----------------------+
| dayofweek(curdate()) |
+----------------------+
| 1 |
+----------------------+
1 row in set (0.00 sec)
10.dayofmonth(x) 计算日期x是本月的第几天
mysql> select dayofmonth(curdate());
+-----------------------+
| dayofmonth(curdate()) |
+-----------------------+
| 13 |
+-----------------------+
1 row in set (0.00 sec)
11.dayofyear(x) 计算日期x是本年的第几天
mysql> select dayofyear(curdate());
+----------------------+
| dayofyear(curdate()) |
+----------------------+
| 257 |
+----------------------+
1 row in set (0.00 sec)
欢迎留言讨论。