MySQL数据库函数与MySQL数据库存储过程!

Posted handsomeboy-东

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL数据库函数与MySQL数据库存储过程!相关的知识,希望对你有一定的参考价值。

mysql的函数及存储过程

一、MySQL数据库函数

概述:用于数据库中进行一系列的算是操作,主要有数学函数、聚合函数、字符串函数、日期时间函数

1.1数学函数

abs(x) :					#返回x的绝对值
rand() :					#返回01的随机数
mod(x,y) :					#返回x除以y的余数
power(x,y) :				#返回x的y次方
round(x) :					#返回离x最近的整数
rount(x,y) :				#保留x的y位小数四舍五入后的值
sqrt(x) :					#返回x的平方跟
truncate(x,y) :				#返回数字x截取为y为小数的值
ceil(x) :					#返回大于或等于x的最小整数
floor(x) :					#返回小于或者等于x的最大整数
qreatest(x1,x2...) :		#返回集合中最大的值
least(x1,x2...) :			#返回集合中最小的值
  • 函数abs()
mysql> select abs(-1);
+---------+
| abs(-1) |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

mysql> select abs(0);
+--------+
| abs(0) |
+--------+
|      0 |
+--------+
1 row in set (0.00 sec)
  • 函数rand()
mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.7838600853307732 |
+--------------------+
1 row in set (0.00 sec)

mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.4494626339985641 |
+--------------------+
1 row in set (0.00 sec)
  • 函数mod(x,y)
mysql> select mod(5,2);
+----------+
| mod(5,2) |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)
  • 函数power(x,y)
mysql> select power(2,3);
+------------+
| power(2,3) |
+------------+
|          8 |
+------------+
1 row in set (0.00 sec)
  • 函数round(x)
mysql> select round(1.49);
+-------------+
| round(1.49) |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)

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

mysql> select round(1.53);
+-------------+
| round(1.53) |
+-------------+
|           2 |
+-------------+
1 row in set (0.00 sec)
  • 函数rount(x,y)
mysql> select round(1.555,2);
+----------------+
| round(1.555,2) |
+----------------+
|           1.56 |
+----------------+
1 row in set (0.00 sec)

mysql> select round(1.5,2);
+--------------+
| round(1.5,2) |
+--------------+
|         1.50 |
+--------------+
1 row in set (0.00 sec)
  • 函数sqrt(x)
mysql> select sqrt(4);
+---------+
| sqrt(4) |
+---------+
|       2 |
+---------+
1 row in set (0.00 sec)

mysql> select sqrt(9);
+---------+
| sqrt(9) |
+---------+
|       3 |
+---------+
1 row in set (0.00 sec)
  • 函数truncate(x,y)
mysql> select truncate(1.555,2);			#截断小数点后两位
+-------------------+
| truncate(1.555,2) |
+-------------------+
|              1.55 |
+-------------------+
1 row in set (0.00 sec)
  • 函数ceil(x)
mysql> select ceil(5.2);
+-----------+
| ceil(5.2) |
+-----------+
|         6 |
+-----------+
1 row in set (0.00 sec)

mysql> select ceil(5.0);
+-----------+
| ceil(5.0) |
+-----------+
|         5 |
+-----------+
1 row in set (0.00 sec)
  • 函数floor(x)
mysql> select floor(5.2);
+------------+
| floor(5.2) |
+------------+
|          5 |
+------------+
1 row in set (0.00 sec)

mysql> select floor(5.0);
+------------+
| floor(5.0) |
+------------+
|          5 |
+------------+
1 row in set (0.00 sec)
  • 函数qreatest(x1,x2…)
mysql> select greatest(1,2,3);
+-----------------+
| greatest(1,2,3) |
+-----------------+
|               3 |
+-----------------+
1 row in set (0.00 sec)
  • 函数least(x1,x2…)
mysql> select least(1,2,3);
+--------------+
| least(1,2,3) |
+--------------+
|            1 |
+--------------+
1 row in set (0.01 sec)

1.2聚合函数

avg() :				#返回指定列的平均值
count() :			#返回指定列中非null值的个数
min() :				#返回指定列的最小值
max() :				#返回指定列的最大值
sum() :				#返回指定列的所有值之和
  • 函数avg()
mysql> select * from whd;
+------+-----------+------+-------+------+------+
| id   | name      | age  | score | addr | sss  |
+------+-----------+------+-------+------+------+
|    1 | zhangshan |   18 |    80 | null | NULL |
|    2 | lisi      |   20 |    60 |      | NULL |
|    3 | wangwu    |   25 |    60 | ai   | NULL |
|    4 | wangmazi  |   22 |    90 | ai   | NULL |
|    5 | xuyi      |   18 |    90 | ai   | NULL |
+------+-----------+------+-------+------+------+
5 rows in set (0.00 sec)

mysql> select avg(score) from whd;
+------------+
| avg(score) |
+------------+
|    76.0000 |
+------------+
1 row in set (0.00 sec)
  • 函数count()
mysql> select count(id) from whd;
+-----------+
| count(id) |
+-----------+
|         5 |
+-----------+
1 row in set (0.00 sec)
  • 函数min ()
mysql> select min(score) from whd;
+------------+
| min(score) |
+------------+
|         60 |
+------------+
1 row in set (0.00 sec)
  • 函数max()
mysql> select max(score) from whd;
+------------+
| max(score) |
+------------+
|         90 |
+------------+
1 row in set (0.00 sec)
  • 函数sum()
mysql> select sum(score) from whd;
+------------+
| sum(score) |
+------------+
|        380 |
+------------+
1 row in set (0.00 sec)

1.3字符串函数

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个字符
space(x) :					#返回x个空格
replace(x,y,z) :			#将字符串z替代字符串x中的字符串y
strcmp(x,y) :				#比较x和y,返回的值可以为-101
substring(x,y,z) :			#获取从字符串x中的第y个位置开始长度为z的字符串
reverse(x) :				#将字符串x反转
repeat(x,y) :				#将字符串x重复y次
  • 函数length(x)
mysql> select length('abcd');
+----------------+
| length('abcd') |
+----------------+
|              4 |
+----------------+
1 row in set (0.00 sec)

mysql> select length('ab cd');
+-----------------+
| length('ab cd') |
+-----------------+
|               5 |
+-----------------+
1 row in set (0.00 sec)
  • 函数trim()
mysql> select trim('    abcd     ');			#去除头尾空格
+-----------------------+
| trim('    abcd     ') |
+-----------------------+
| abcd                  |
+-----------------------+
1 row in set (0.00 sec)

mysql> select length(trim('  abc  '));
+-------------------------+
| length(trim('  abc  ')) |
+-------------------------+
|                       3 |
+-------------------------+
1 row in set (0.00 sec)
  • 函数concat(x,y)
mysql> select concat('abc','df');
+--------------------+
| concat('abc','df') |
+--------------------+
| abcdf              |
+--------------------+
1 row in set (0.00 sec)

mysql> select concat('abc ',' df');
+----------------------+
| concat('abc ',' df') |
+----------------------+
| abc  df              |
+----------------------+
1 row in set (0.00 sec)

mysql> select concat('abc ',trim(' df'));
+----------------------------+
| concat('abc ',trim(' df')) |
+----------------------------+
| abc df                     |
+----------------------------+
1 row in set (0.00 sec)

mysql> select concat(left('abcdefg',3),right('12345',3));
+--------------------------------------------+
| concat(left('abcdefg',3),right('12345',3)) |
+--------------------------------------------+
| abc345                                     |
+--------------------------------------------+
1 row in set (0.00 sec)

  • 函数upper(x)
mysql> select upper('aBc');
+--------------+
| upper('aBc') |
+--------------+
| ABC          |
+--------------+
1 row in set (0.00 sec)
  • 函数lower(x)
mysql> select lower('aBc');
+--------------+
| lower('aBc') |
+--------------+
| abc          |
+--------------+
1 row in set (0.00 sec)
  • 函数left(x,y)
mysql> select left('abcdefg',3);
+-------------------+
| left('abcdefg',3) |
+-------------------+
| abc               |
+-------------------+
1 row in set (0.01 sec)
  • 函数right(x,y)
mysql> select right('abcdefg',3);
+--------------------+
| right('abcdefg',3) |
+--------------------+
| efg                |
+--------------------+
1 row in set (0.00 sec)
  • 函数space(x)
mysql> select length(space(5));
+------------------+
| length(space(5)) |
+------------------+
|                5 |
+------------------+
1 row in set (0.00 sec)
  • 函数replace(x,y,z)
mysql> select replace('hello','ll','bb');
+----------------------------+
| replace('hello','ll','bb') |
+----------------------------+
| hebbo                      |
+----------------------------+
1 row in set (0.00 sec)
  • 函数strcmp(x,y)
mysql> select strcmp(5,6);
+-------------+
| strcmp(5,6) |
+-------------+
|          -1 |
+-------------+
1 row in set (0.00 sec)

mysql> select strcmp(6,6);
+-------------+
| strcmp(6,6) |
+-------------+
|           0 |
+-------------+
1 row in set (0.00 sec)

mysql> select strcmp(7,6);
+-------------+
| strcmp(7,6) |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)
  • 函数substring(x,y,z)
mysql> select substring('abcdefg',3,4);
+--------------------------+
| substring('abcdefg',3,4) |
+--------------------------+
| cdef                     |
+--------------------------+
1 row in set (0.00 sec)
  • 函数reverse(x)
mysql> select reverse('abcd');
+-----------------+
| reverse('abcd') |
+-----------------+
| dcba            |
+-----------------+
1 row in set (0.00 sec)

mysql> select reverse('1234');
+-----------------+
| reverse('1234') |
+-----------------+
| 4321            |
+-----------------+
1 row in set (0.00 sec)
  • 函数repeat(x)
mysql> select repeat('abc',3);
+-----------------+
| repeat('abc',3) |
+-----------------+
| abcabcabc       |
+-----------------+
1 row in set (0.00 sec)

1.4日期时间函数

curdate() :						#返回当前时间的年月日
curtime() :						#返回当前时间的时分秒
now() :							#返回当前时间的日期和时间
month(x) :						#返回日期x中的月份值
week(x) :						#返回x是年度第几个星期
hour(x) :						#返回x中的小时值
minute(x) :						#返回x中的分钟值
second(x) :						#返回x中的秒钟值
dayofweek(x) :					#返回x是星期几,1为星期日,2为星期一
replace(x,y,z) :				#将字符串z替代字符串x中的字符串y
dayofmonth(x) :					#计算日志x是本月的第几天
dayofyear(x) :					#计算日期x是本年的第几天
  • 函数curdate()
mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2021-07-17 |
+------------+
1 row in set (0.00 sec)
  • 函数curtime()
mysql> select curtime(MySQL数据库之存储过程与存储函数

mysql 存储过程与存储函数

mysql 与oracle中的存储过程及函数有啥区别,尽可能详细哦

为啥mysql创建存储函数,会是这样报错

mysql为四个表创建储存过程或者储存函数

MySql存储过程与函数