MySQL函数大合集

MySQL函数大合集

一,数值函数

(1) 绝对值函数

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

(2) 求余函数

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

(3) 获取整数的函数

mysql> select ceil(123.123);	#向上取整
+---------------+
| ceil(123.123) |
+---------------+
|           124 |
+---------------+
1 row in set (0.00 sec)
mysql> select floor(123.123);	#向下取整
+----------------+
| floor(123.123) |
+----------------+
|            123 |
+----------------+
1 row in set (0.00 sec)

(4) 获取随机数的函数

mysql> select rand(); #0~1随机数
+-------------------+
| rand()            |
+-------------------+
| 0.509076374125738 |
+-------------------+
1 row in set (0.00 sec)

(5) 四舍五入的函数

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

(6) 截取数值的函数

mysql> select truncate(123.123,2);  #第二个数值,代表截取长度
+---------------------+
| truncate(123.123,2) |
+---------------------+
|              123.12 |
+---------------------+
1 row in set (0.00 sec)

(7) 符号函数

mysql> select sign(-1);	#如果是负数对应的-1 整数对应的是1 0对应的是0
+----------+
| sign(-1) |
+----------+
|       -1 |
+----------+
1 row in set (0.00 sec)

(8) 幂运算函数

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

二,字符串函数

(9) 计算字符串长度的函数

mysql> select length('123');	#查看字节数
+---------------+
| length('123') |
+---------------+
|             3 |
+---------------+
1 row in set (0.00 sec)

mysql> select char_length('123');	#查看字符串的长度
+--------------------+
| char_length('123') |
+--------------------+
|                  3 |
+--------------------+
1 row in set (0.00 sec)

(10) 合并字符串的函数

mysql> select concat('hello','world');
+-------------------------+
| concat('hello','world') |
+-------------------------+
| helloworld              |
+-------------------------+
1 row in set (0.00 sec)

mysql> select concat_ws('-','hello','world');	#可以指定分割符
+--------------------------------+
| concat_ws('-','hello','world') |
+--------------------------------+
| hello-world                    |
+--------------------------------+
1 row in set (0.00 sec)

(11) 替换字符串的函数

mysql> select insert('hello',1,1,'H');	#第二个字段指定开始位置,第三个字段解锁位置 第四个字段,替换的值
+-------------------------+
| insert('hello',1,1,'H') |
+-------------------------+
| Hello                   |
+-------------------------+
1 row in set (0.00 sec)

(12) 转换大小写的函数:

mysql> select lower('Hello');	#转为小写
+----------------+
| lower('Hello') |
+----------------+
| hello          |
+----------------+
1 row in set (0.00 sec)

mysql> select upper('Hello');	#转为大写
+----------------+
| upper('Hello') |
+----------------+
| HELLO          |
+----------------+
1 row in set (0.00 sec)

(13) 获取指定长度的字符串的函数:

mysql> select left('hello',3);	#输出从左的长度
+-----------------+
| left('hello',3) |
+-----------------+
| hel             |
+-----------------+
1 row in set (0.00 sec)

mysql> select right('hello',3);	#输出从右的长度
+------------------+
| right('hello',3) |
+------------------+
| llo              |
+------------------+
1 row in set (0.00 sec)

(14) 填充字符串的函数:

mysql> select lpad('hello',10,'@');	#如果不满足指定长度填充
+----------------------+
| lpad('hello',10,'@') |
+----------------------+
| @@@@@hello           |
+----------------------+
1 row in set (0.00 sec)

mysql> select rpad('hello',10,'@');
+----------------------+
| rpad('hello',10,'@') |
+----------------------+
| hello@@@@@           |
+----------------------+
1 row in set (0.00 sec)

(15) 删除空格的函数:

mysql> select ltrim('    hello     ');	#从左删除空格
+-------------------------+
| ltrim('    hello     ') |
+-------------------------+
| hello                   |
+-------------------------+
1 row in set (0.00 sec)

mysql> select rtrim('    hello     '); #从右删除空格
+-------------------------+
| rtrim('    hello     ') |
+-------------------------+
|     hello               |
+-------------------------+
1 row in set (0.00 sec)

mysql> select trim('    hello     ');	#从两侧删除空格
+------------------------+
| trim('    hello     ') |
+------------------------+
| hello                  |
+------------------------+
1 row in set (0.00 sec)

(16) 删除指定字符串的函数:

mysql> select trim('h' from 'hello');	#指定字段进行删除
+------------------------+
| trim('h' from 'hello') |
+------------------------+
| ello                   |
+------------------------+
1 row in set (0.00 sec)

(17) 重复生成字符串的函数:

mysql> select repeat('233',2);	#将
+-----------------+
| repeat('233',2) |
+-----------------+
| 233233          |
+-----------------+
1 row in set (0.00 sec)

(18) 空格函数:

mysql> select space(5);
+-------+
| l     |
+-------+
|       |
+-------+
1 row in set (0.00 sec)

(19) 替换函数:

mysql> select replace('hello','h','H');
+--------------------------+
| replace('hello','h','H') |
+--------------------------+
| Hello                    |
+--------------------------+
1 row in set (0.00 sec)

(20) 比较字符串大小的函数:

mysql> select strcmp('a','C');
+-----------------+
| strcmp('a','C') |
+-----------------+
|              -1 |
+-----------------+
1 row in set (0.00 sec)

(21) 获取子字符串的函数:

mysql> select mid('hello',1,3);
+------------------+
| mid('hello',1,3) |
+------------------+
| hel              |
+------------------+
1 row in set (0.00 sec)

(22) 匹配子字符串开始位置的函数:

mysql> select locate('e','hello');
+---------------------+
| locate('e','hello') |
+---------------------+
|                   2 |
+---------------------+
1 row in set (0.00 sec)

(23) 反转字符串的函数:

mysql> select reverse('12345');
+------------------+
| reverse('12345') |
+------------------+
| 54321            |
+------------------+
1 row in set (0.00 sec)

(24) 返回指定位置的字符串的函数:

mysql> select elt(2,'a','b','c');
+--------------------+
| elt(2,'a','b','c') |
+--------------------+
| b                  |
+--------------------+
1 row in set (0.00 sec)

(25) 返回指定字符串位置的函数:

mysql> select field('a','a','b','c');
+------------------------+
| field('a','a','b','c') |
+------------------------+
|                      1 |
+------------------------+
1 row in set (0.00 sec)

(26) 返回子字符串位置的函数

mysql> select find_in_set('c','a,b,c,d');
+----------------------------+
| find_in_set('c','a,b,c,d') |
+----------------------------+
|                          3 |
+----------------------------+
1 row in set (0.00 sec)

三,日期函数

(27) 获取当前日期的函数

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2023-04-14 |
+------------+
1 row in set (0.00 sec)

(28) 获取当前时间的函数

mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 08:54:47  |
+-----------+
1 row in set (0.00 sec)

(29) 获取当前日期和时间的函数

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2023-04-14 08:54:51 |
+---------------------+
1 row in set (0.00 sec)

(30) 获取时间戳的函数:

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

(31) 转换时间戳的函数:

mysql> select from_unixtime(1681433706);
+---------------------------+
| from_unixtime(1681433706) |
+---------------------------+
| 2023-04-14 08:55:06       |
+---------------------------+
1 row in set (0.00 sec)

(32) 获取 UTC 日期的函数:

mysql> select utc_date;
+------------+
| utc_date   |
+------------+
| 2023-04-14 |
+------------+
1 row in set (0.00 sec)

(33) 获取 UTC 时间的函数:

mysql> select utc_time;
+----------+
| utc_time |
+----------+
| 00:55:38 |
+----------+
1 row in set (0.00 sec)

(34) 获取月份的函数:

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

(35) 获取星期的函数:

mysql> select dayname(now());	#英文显示第周几
+----------------+
| dayname(now()) |
+----------------+
| Friday         |
+----------------+
1 row in set (0.00 sec)

mysql> select dayofweek(now());	#一周中的第多少天  1 表示周日,2 表示周一,...... ,7 表示周六

+------------------+
| dayofweek(now()) |
+------------------+
|                6 |
+------------------+
1 row in set (0.00 sec)

mysql> select weekday(now()); #0 表示周一,1 表示周二,...... ,6 表示周日
+----------------+
| weekday(now()) |
+----------------+
|              4 |
+----------------+
1 row in set (0.00 sec)

(36) 获取天数的函数

mysql> select dayofyear(now());		#一年中的第多少天
+------------------+
| dayofyear(now()) |
+------------------+
|              104 |
+------------------+
1 row in set (0.00 sec)

mysql> select dayofmonth(now());	#一个月中的第多少天
+-------------------+
| dayofmonth(now()) |
+-------------------+
|                14 |
+-------------------+
1 row in set (0.00 sec)

(37) 获取年份的函数:

mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
|        2023 |
+-------------+
1 row in set (0.00 sec)

(38) 获取季度的函数

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

(39) 获取分钟的函数:

mysql> select minute(now());
+---------------+
| minute(now()) |
+---------------+
|            57 |
+---------------+
1 row in set (0.00 sec)

(40) 获取秒钟的函数:

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

(41) 获取日期的指定值的函数

mysql> select extract(year from now());
+--------------------------+
| extract(year from now()) |
+--------------------------+
|                     2023 |
+--------------------------+
1 row in set (0.00 sec)

mysql> select extract(year_month from now());
+--------------------------------+
| extract(year_month from now()) |
+--------------------------------+
|                         202304 |
+--------------------------------+
1 row in set (0.00 sec)

mysql> select extract(hour_minute from now());
+---------------------------------+
| extract(hour_minute from now()) |
+---------------------------------+
|                             859 |
+---------------------------------+
1 row in set (0.00 sec)

(42) 时间和秒钟转换的函数

mysql> select time_to_sec(now());
+--------------------+
| time_to_sec(now()) |
+--------------------+
|              32874 |
+--------------------+
1 row in set (0.00 sec)

mysql> select sec_to_time(32874);
+--------------------+
| sec_to_time(32874) |
+--------------------+
| 09:07:54           |
+--------------------+
1 row in set (0.00 sec)

(43) 计算日期和时间的函数

mysql> select date_add(now(),interval 1 year);		#当前日期增加一年
+---------------------------------+
| date_add(now(),interval 1 year) |
+---------------------------------+
| 2024-04-14 09:09:30             |
+---------------------------------+
1 row in set (0.00 sec)

mysql> select date_sub(now(),interval 1 month);		#当前日期减一个月
+----------------------------------+
| date_sub(now(),interval 1 month) |
+----------------------------------+
| 2023-03-14 09:09:58              |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select datediff(now(),'2023-03-14 09:09:58');	#取两个时间的差
+---------------------------------------+
| datediff(now(),'2023-03-14 09:09:58') |
+---------------------------------------+
|                                    31 |
+---------------------------------------+
1 row in set (0.00 sec)

(44) 将日期和时间格式化的函数

mysql> select date_format(now(),'%y %m %d');
+-------------------------------+
| date_format(now(),'%y %m %d') |
+-------------------------------+
| 23 04 14                      |
+-------------------------------+
1 row in set (0.00 sec)

mysql> select date_format(now(),get_format(date,'usa'));
+-------------------------------------------+
| date_format(now(),get_format(date,'usa')) |
+-------------------------------------------+
| 04.14.2023                                |
+-------------------------------------------+
1 row in set (0.00 sec)

四,逻辑函数

(45) IF() #判断添加是否正确,正确返回第一个值,错误返回第二个值

mysql> select if(1>2,'true','false');
+------------------------+
| if(1>2,'true','false') |
+------------------------+
| false                  |
+------------------------+
1 row in set (0.00 sec)

mysql> select if(1<2,'true','false');
+------------------------+
| if(1<2,'true','false') |
+------------------------+
| true                   |
+------------------------+
1 row in set (0.00 sec)

(46) IFNULL() #判断字符串是否为空

mysql> select ifnull(null,'true');
+---------------------+
| ifnull(null,'true') |
+---------------------+
| true                |
+---------------------+
1 row in set (0.00 sec)

(47) CASE

mysql> select case 1 when 1 then 'one' end;
+------------------------------+
| case 1 when 1 then 'one' end |
+------------------------------+
| one                          |
+------------------------------+
1 row in set (0.00 sec)

mysql> select case 1 when 1 then 'one' when 2 then 'two' end;
+------------------------------------------------+
| case 1 when 1 then 'one' when 2 then 'two' end |
+------------------------------------------------+
| one                                            |
+------------------------------------------------+
1 row in set (0.00 sec)

mysql> select case 1 when 1 then 'one' when 2 then 'two' else 'no' end;
+----------------------------------------------------------+
| case 1 when 1 then 'one' when 2 then 'two' else 'no' end |
+----------------------------------------------------------+
| one                                                      |
+----------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select case 3 when 1 then 'one' when 2 then 'two' else 'no' end;
+----------------------------------------------------------+
| case 3 when 1 then 'one' when 2 then 'two' else 'no' end |
+----------------------------------------------------------+
| no                                                       |
+----------------------------------------------------------+
1 row in set (0.00 sec)

五,系统函数

(48) 获取 MySQL 版本号的函数:

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

(49) 查看当前用户的连接数的ID函数:

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

(50) 查看当前使用的数据库的函数:

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

(51) 查看当前登录的用户名的函数:

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> show processlist;		#查看系统当前所有的登录用户的详细信息
+----+------+-----------+------+---------+------+----------+------------------+
| Id | User | Host      | db   | Command | Time | State    | Info             |
+----+------+-----------+------+---------+------+----------+------------------+
|  3 | root | localhost | NULL | Query   |    0 | starting | show processlist |
+----+------+-----------+------+---------+------+----------+------------------+
1 row in set (0.00 sec)

(52) 查看指定字符串的字符集的函数:

mysql> select charset('a');
+--------------+
| charset('a') |
+--------------+
| utf8         |
+--------------+
1 row in set (0.01 sec)

(53) 查看指定字符串的排列方式的函数:

mysql> select collation('a');
+-----------------+
| collation('a')  |
+-----------------+
| utf8_general_ci |
+-----------------+
1 row in set (0.01 sec)

(54) 获取最后一个自动生成的 ID 值得函数

mysql> select last_insert_id();		#查看当前库中最后一个插入库中的最后一行的ID
+------------------+
| last_insert_id() |
+------------------+
|                0 |
+------------------+
1 row in set (0.00 sec)

六,加密/解密函数

(55) 加密函数

mysql> select password('123');		#使用password加密方法进行加密
+-------------------------------------------+
| password('123')                           |
+-------------------------------------------+
| *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> select md5('123');		#使用md5进行加密
+----------------------------------+
| md5('123')                       |
+----------------------------------+
| 202cb962ac59075b964b07152d234b70 |
+----------------------------------+
1 row in set (0.00 sec)

mysql> select encode('123','test');	#使用test进行加密
+----------------------+
| encode('123','test') |
+----------------------+
| ~5                   |
+----------------------+
1 row in set, 1 warning (0.00 sec)

(56) 解密函数 #将123使用test来进行加密 然后通过test进行解密

mysql> select decode(encode('123','test'),'test');
+-------------------------------------+
| decode(encode('123','test'),'test') |
+-------------------------------------+
| 123                                 |
+-------------------------------------+
1 row in set, 2 warnings (0.00 sec)

七,其他函数

(57) 格式化函数:

mysql> select format(123.123,4);
+-------------------+
| format(123.123,4) |
+-------------------+
| 123.1230          |
+-------------------+
1 row in set (0.00 sec)

(58) 不同进制的数字进行转换的函数: #将二进制转为16进制

mysql> select conv('101010111101000',2,16);
+------------------------------+
| conv('101010111101000',2,16) |
+------------------------------+
| 55E8                         |
+------------------------------+
1 row in set (0.00 sec)

(59) IP 地址与数字互相转换的函数:

mysql> select inet_aton('127.0.0.1');
+------------------------+
| inet_aton('127.0.0.1') |
+------------------------+
|             2130706433 |
+------------------------+
1 row in set (0.00 sec)

mysql> select inet_ntoa('2130706433');
+-------------------------+
| inet_ntoa('2130706433') |
+-------------------------+
| 127.0.0.1               |
+-------------------------+
1 row in set (0.00 sec)

(60) 重复执行指定操作的函数:

mysql> select benchmark(1000,password('123'));
+---------------------------------+
| benchmark(1000,password('123')) |
+---------------------------------+
|                               0 |
+---------------------------------+
1 row in set, 1 warning (0.00 sec)

(61) 改变字符集的函数:

mysql> select convert('a' using 'gb18030');
+------------------------------+
| convert('a' using 'gb18030') |
+------------------------------+
| a                            |
+------------------------------+
1 row in set (0.00 sec)

(62) 改变数据类型的函数:

mysql> select convert(123,char(2));
+----------------------+
| convert(123,char(2)) |
+----------------------+
| 12                   |
+----------------------+
1 row in set, 1 warning (0.00 sec)

你可能感兴趣的:(从精通到忘记学习数据库,mysql,数据库)