一、数学函数
- abs(x) :返回x的绝对值
- rand() :返回0到1的随机数 注意:取不到1
- mod(x,y):返回x除以y以后的余数
- power(x,y):返回x的y次方
- round(x):返回离x最近的整数
- sqrt(x):返回x的平方根
- truncate(x,y):返回数字x截断为y位小数的值
- ceil(x):返回大于或等于x的最小整数 ## 注意:是向上取整数
- floor(x):返回小于或等于x的最大整数 ## 注意:是向下取整数
- greatest(x1,x2…):返回集合中最大的值
- least(x1,x2…):返回集合中最小的值
例:
mysql> select abs(-9);
+---------+
| abs(-9) |
+---------+
| 9 |
+---------+
1 row in set (0.00 sec)
mysql> select rand();
+---------------------+
| rand() |
+---------------------+
| 0.05251197149279711 |
+---------------------+
1 row in set (0.00 sec)
mysql> select mod(6,4);
+----------+
| mod(6,4) |
+----------+
| 2 |
+----------+
1 row in set (0.00 sec)
mysql> select power(2,2);
+------------+
| power(2,2) |
+------------+
| 4 |
+------------+
1 row in set (0.00 sec)
mysql> select round(2.6);
+------------+
| round(2.6) |
+------------+
| 3 |
+------------+
1 row in set (0.00 sec)
mysql> select sqrt(9);
+---------+
| sqrt(9) |
+---------+
| 3 |
+---------+
1 row in set (0.00 sec)
mysql> select truncate(3.1415,2);
+--------------------+
| truncate(3.1415,2) |
+--------------------+
| 3.14 |
+--------------------+
1 row in set (0.00 sec)
mysql> select ceil(1.23);
+------------+
| ceil(1.23) |
+------------+
| 2 |
+------------+
1 row in set (0.00 sec)
mysql> select floor(1.57);
+-------------+
| floor(1.57) |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
mysql> select greatest(12,7,9,10);
+---------------------+
| greatest(12,7,9,10) |
+---------------------+
| 12 |
+---------------------+
1 row in set (0.00 sec)
mysql> select least(12,7,9,10);
+------------------+
| least(12,7,9,10) |
+------------------+
| 7 |
+------------------+
1 row in set (0.00 sec)
二、聚合函数
- 对表中数据记录进行集中概括而设计的一类函数
- 常用的聚合函数
- avg():返回指定列的平均值
- count():返回指定列中非NULL值的个数
- min():返回指定列的最小值
- max():返回指定列的最大值
- sum():返回指定列的所有值之和
例:因在之前的博文中详细说明过,这里就仅举个例
mysql> select avf(id) from testtable; ## 返回testtable表中id字段的平均值,其他几个函数用法同此
三、字符串函数
常用的字符串函数
-
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(z,y,z):获取从字符串x中的第y个位置开始长度为z的字符串
-
reverse(x):将字符串x反转
例:
mysql> select length('abcdefg');
+-------------------+
| length('abcdefg') |
+-------------------+
| 7 |
+-------------------+
1 row in set (0.00 sec)
mysql> select trim(' abc '); ## 只能前后空格
+----------------------+
| trim(' abc ') |
+----------------------+
| abc |
+----------------------+
1 row in set (0.00 sec)
mysql> select concat('abc','def');
+---------------------+
| concat('abc','def') |
+---------------------+
| abcdef |
+---------------------+
1 row in set (0.00 sec)
mysql> select upper('abc');
+--------------+
| upper('abc') |
+--------------+
| ABC |
+--------------+
1 row in set (0.00 sec)
mysql> select lower('ABC');
+--------------+
| lower('ABC') |
+--------------+
| abc |
+--------------+
1 row in set (0.00 sec)
mysql>
mysql> select left('abcde',3);
+-----------------+
| left('abcde',3) |
+-----------------+
| abc |
+-----------------+
1 row in set (0.00 sec)
mysql> select right('abcde',3);
+------------------+
| right('abcde',3) |
+------------------+
| cde |
+------------------+
1 row in set (0.00 sec)
mysql> select concat('a',space(3),'b');
+--------------------------+
| concat('a',space(3),'b') |
+--------------------------+
| a b |
+--------------------------+
1 row in set (0.00 sec)
mysql> select replace('abc','b','d');
+------------------------+
| replace('abc','b','d') |
+------------------------+
| adc |
+------------------------+
1 row in set (0.00 sec)
mysql> select strcmp(1,2);
+-------------+
| strcmp(1,2) |
+-------------+
| -1 |
+-------------+
1 row in set (0.00 sec)
mysql> select strcmp(6,3);
+-------------+
| strcmp(6,3) |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
mysql> select strcmp(5,5);
+-------------+
| strcmp(5,5) |
+-------------+
| 0 |
+-------------+
1 row in set (0.00 sec)
四、日期和时间函数
常用 日期函数:
- curdate(x):返回当前时间的年月日
- curtime(x):返回当前时间的时分秒
- now(x):返回房钱时间的日期和时间
- month(x):返回日期x中的月份值
- week(x):返回日期x是年度第几个星期
- hour(x):返回x中的小时值
- minute(x):返回x中的分钟值
- second(x):返回x中的秒钟值
- dayofweek(x):返回x是星期几,1星期日,2星期一
- dayofmonth(x):计算日期x是本月的第几天
- dayofyear(x):(x):计算日期x是本年的第几天
例:
mysql> select curdate(),curtime(),now();
+------------+-----------+---------------------+
| curdate() | curtime() | now() |
+------------+-----------+---------------------+
| 2020-08-28 | 20:36:56 | 2020-08-28 20:36:56 |
+------------+-----------+---------------------+
1 row in set (0.01 sec)
mysql> select month('2020-08-28'),week('2020-08-28');
+---------------------+--------------------+
| month('2020-08-28') | week('2020-08-28') |
+---------------------+--------------------+
| 8 | 34 |
+---------------------+--------------------+
1 row in set (0.00 sec)
mysql> select hour('20:38:40'),minute('20:38:40');
+------------------+--------------------+
| hour('20:38:40') | minute('20:38:40') |
+------------------+--------------------+
| 20 | 38 |
+------------------+--------------------+
1 row in set (0.00 sec)
mysql> select second('20:38:40'),dayofweek('2020-08-28');
+--------------------+-------------------------+
| second('20:38:40') | dayofweek('2020-08-28') |
+--------------------+-------------------------+
| 40 | 6 |
+--------------------+-------------------------+
1 row in set (0.00 sec)
mysql> select dayofmonth('2020-08-28'),dayofyear('2020-08-28');
+--------------------------+-------------------------+
| dayofmonth('2020-08-28') | dayofyear('2020-08-28') |
+--------------------------+-------------------------+
| 28 | 241 |
+--------------------------+-------------------------+
1 row in set (0.00 sec)