【MySQL】内置函数

【MySQL】内置函数

日期函数

  1. 获取年月日
sql 复制代码
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2024-08-12     |
+----------------+
1 row in set (0.01 sec)
  1. 获取时分秒
sql 复制代码
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 13:53:48       |
+----------------+
1 row in set (0.00 sec)
  1. 获取时间戳
sql 复制代码
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2024-08-12 13:54:15 |
+---------------------+
1 row in set (0.01 sec)
sql 复制代码
mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-08-12 13:54:46 |
+---------------------+
1 row in set (0.00 sec)
  1. 返回日期部分
sql 复制代码
mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2024-08-12  |
+-------------+
1 row in set (0.00 sec)
sql 复制代码
mysql> select date(current_timestamp());
+---------------------------+
| date(current_timestamp()) |
+---------------------------+
| 2024-08-12                |
+---------------------------+
1 row in set (0.00 sec)
  1. 在日期的基础上加上日期
sql 复制代码
mysql> select date_add('2024-10-1',interval 10 day);
+---------------------------------------+
| date_add('2024-10-1',interval 10 day) |
+---------------------------------------+
| 2024-10-11                            |
+---------------------------------------+
1 row in set (0.00 sec)

mysql> select date_add('2024-10-1',interval 10 year);
+----------------------------------------+
| date_add('2024-10-1',interval 10 year) |
+----------------------------------------+
| 2034-10-01                             |
+----------------------------------------+
1 row in set (0.00 sec)
  1. 在日期的基础上减上日期
sql 复制代码
mysql> select date_sub('2024-10-1',interval 10 day);
+---------------------------------------+
| date_sub('2024-10-1',interval 10 day) |
+---------------------------------------+
| 2024-09-21                            |
+---------------------------------------+
1 row in set (0.00 sec)

mysql> select date_sub('2024-10-1',interval 10 year);
+----------------------------------------+
| date_sub('2024-10-1',interval 10 year) |
+----------------------------------------+
| 2014-10-01                             |
+----------------------------------------+
1 row in set (0.01 sec)
  1. 计算连个日期的间隔天数
sql 复制代码
mysql> select datediff(now(),'1949-10-1');
+-----------------------------+
| datediff(now(),'1949-10-1') |
+-----------------------------+
|                       27344 |
+-----------------------------+
1 row in set (0.00 sec)

字符串函数

  1. charset查看使用的字符集
sql 复制代码
mysql> select charset(ename) from emp;
+----------------+
| charset(ename) |
+----------------+
| utf8mb4        |
+----------------+
1 rows in set (0.00 sec)
  1. concat拼接字符串
sql 复制代码
mysql> select concat('aaa','bbb',112);
+-------------------------+
| concat('aaa','bbb',112) |
+-------------------------+
| aaabbb112               |
+-------------------------+
1 row in set (0.01 sec)

mysql> select concat(name,'的总分:',chinese+math+english,',语文成绩:',chinese,',数学成绩:',math,',英语成绩:',english) as msg from exam;
+-------------------------------------------------------------------------+
| msg                                                                     |
+-------------------------------------------------------------------------+
| 唐三藏的总分:288,语文成绩:134,数学成绩:98,英语成绩:56                   |
| 猪悟能的总分:364,语文成绩:176,数学成绩:98,英语成绩:90                   |
| 曹孟德的总分:297,语文成绩:140,数学成绩:90,英语成绩:67                   |
| 孙权的总分:291,语文成绩:140,数学成绩:73,英语成绩:78                     |
| 宋公明的总分:275,语文成绩:150,数学成绩:95,英语成绩:30                   |
+-------------------------------------------------------------------------+
5 rows in set (0.00 sec)
  1. instr返回字符串所处的位置
sql 复制代码
mysql> select instr('abcdefg123dg',123);
+---------------------------+
| instr('abcdefg123dg',123) |
+---------------------------+
|                         8 |
+---------------------------+
1 row in set (0.00 sec)
  1. ucase/lcase大小写转换
sql 复制代码
mysql> select ucase('abcd123ABCD');
+----------------------+
| ucase('abcd123ABCD') |
+----------------------+
| ABCD123ABCD          |
+----------------------+
1 row in set (0.01 sec)

mysql> select lcase('abcd123ABCD');
+----------------------+
| lcase('abcd123ABCD') |
+----------------------+
| abcd123abcd          |
+----------------------+
1 row in set (0.00 sec)
  1. 获取指定长度的字符串left/right
sql 复制代码
mysql> select left('abcdefg',3);
+-------------------+
| left('abcdefg',3) |
+-------------------+
| abc               |
+-------------------+
1 row in set (0.00 sec)

mysql> select right('abcdefg',3);
+--------------------+
| right('abcdefg',3) |
+--------------------+
| efg                |
+--------------------+
1 row in set (0.00 sec)
  1. 查看字符串长度length
sql 复制代码
mysql> select length('abcd');
+----------------+
| length('abcd') |
+----------------+
|              4 |
+----------------+
1 row in set (0.00 sec)

mysql> select name,length(name) from exam;
+-----------+--------------+
| name      | length(name) |
+-----------+--------------+
| 唐三藏    |            9 |
| 猪悟能    |            9 |
| 曹孟德    |            9 |
| 孙权      |            6 |
| 宋公明    |            9 |
+-----------+--------------+
5 rows in set (0.00 sec)
  1. 字符替换replace
    将EMP表中所有名字中有S的替换成'上海'
sql 复制代码
mysql> select ename,replace(ename,'S','上海') from emp;
+--------+-----------------------------+
| ename  | replace(ename,'S','上海')   |
+--------+-----------------------------+
| SMITH  | 上海MITH                    |
| ALLEN  | ALLEN                       |
| WARD   | WARD                        |
| JONES  | JONE上海                    |
| MARTIN | MARTIN                      |
| BLAKE  | BLAKE                       |
| CLARK  | CLARK                       |
| SCOTT  | 上海COTT                    |
| KING   | KING                        |
| TURNER | TURNER                      |
| ADAMS  | ADAM上海                    |
| JAMES  | JAME上海                    |
| FORD   | FORD                        |
| MILLER | MILLER                      |
+--------+-----------------------------+
14 rows in set (0.00 sec)
  1. 字符比较strcmp
sql 复制代码
mysql> select strcmp('abc','abc');
+---------------------+
| strcmp('abc','abc') |
+---------------------+
|                   0 |
+---------------------+
1 row in set (0.01 sec)

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

# a.忽略大小写------跟字符集有关
mysql> select strcmp('abc','ABC');
+---------------------+
| strcmp('abc','ABC') |
+---------------------+
|                   0 |
+---------------------+
1 row in set (0.00 sec)

mysql> select strcmp('123','abc');
+---------------------+
| strcmp('123','abc') |
+---------------------+
|                  -1 |
+---------------------+
1 row in set (0.00 sec)
  1. 字符截取substring
    截取EMP表中ename字段的第二个到第三个字符
sql 复制代码
mysql> select ename,substring(ename,2,2) from emp;
+--------+----------------------+
| ename  | substring(ename,2,2) |
+--------+----------------------+
| SMITH  | MI                   |
| ALLEN  | LL                   |
| WARD   | AR                   |
| JONES  | ON                   |
| MARTIN | AR                   |
| BLAKE  | LA                   |
| CLARK  | LA                   |
| SCOTT  | CO                   |
| KING   | IN                   |
| TURNER | UR                   |
| ADAMS  | DA                   |
| JAMES  | AM                   |
| FORD   | OR                   |
| MILLER | IL                   |
+--------+----------------------+
14 rows in set (0.00 sec)

以首字母小写的方式显示所有员工的姓名

sql 复制代码
mysql> select ename,concat(substring(ename,1,1),lcase(substring(ename,2))) as name from emp;
+--------+--------+
| ename  | name   |
+--------+--------+
| SMITH  | Smith  |
| ALLEN  | Allen  |
| WARD   | Ward   |
| JONES  | Jones  |
| MARTIN | Martin |
| BLAKE  | Blake  |
| CLARK  | Clark  |
| SCOTT  | Scott  |
| KING   | King   |
| TURNER | Turner |
| ADAMS  | Adams  |
| JAMES  | James  |
| FORD   | Ford   |
| MILLER | Miller |
+--------+--------+
14 rows in set (0.01 sec)
  1. 去除空格
sql 复制代码
mysql> select ltrim('    mysql     ') as ltrim;
+------------+
| ltrim      |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

mysql> select rtrim('    mysql     ') as rtrim;
+-----------+
| rtrim     |
+-----------+
|     mysql |
+-----------+
1 row in set (0.00 sec)

mysql> select trim('    mysql     ') as trim;
+-------+
| trim  |
+-------+
| mysql |
+-------+
1 row in set (0.00 sec)

数学函数

  1. abs取绝对值
sql 复制代码
mysql> select abs(-1);
+---------+
| abs(-1) |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)
  1. 进制转换
sql 复制代码
mysql> select bin(2);
+--------+
| bin(2) |
+--------+
| 10     |
+--------+
1 row in set (0.01 sec)

mysql> select hex(20);
+---------+
| hex(20) |
+---------+
| 14      |
+---------+
1 row in set (0.00 sec)
sql 复制代码
mysql> select conv(10,10,2);
+---------------+
| conv(10,10,2) |
+---------------+
| 1010          |
+---------------+
1 row in set (0.00 sec)
  1. 向上,向下取整
sql 复制代码
mysql> select ceiling(3.14);
+---------------+
| ceiling(3.14) |
+---------------+
|             4 |
+---------------+
1 row in set (0.00 sec)

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

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

mysql> select floot(-3.14);
ERROR 1305 (42000): FUNCTION test_db.floot does not exist
mysql> select floor(-3.14);
+--------------+
| floor(-3.14) |
+--------------+
|           -4 |
+--------------+
1 row in set (0.00 sec) 
  1. 格式化,保留小数
sql 复制代码
mysql> select format(3.1415926,2);
+---------------------+
| format(3.1415926,2) |
+---------------------+
| 3.14                |
+---------------------+
1 row in set (0.00 sec)

mysql> select format(3.1415926,4);
+---------------------+
| format(3.1415926,4) |
+---------------------+
| 3.1416              |
+---------------------+
1 row in set (0.00 sec)
  1. 随机值
sql 复制代码
mysql> select rand();
+---------------------+
| rand()              |
+---------------------+
| 0.39838555212913596 |
+---------------------+
1 row in set (0.00 sec)

mysql> select format(rand()*100,1);
+----------------------+
| format(rand()*100,1) |
+----------------------+
| 91.9                 |
+----------------------+
1 row in set (0.02 sec)
  1. 取模求余
sql 复制代码
mysql> select mod(10,3);
+-----------+
| mod(10,3) |
+-----------+
|         1 |
+-----------+
1 row in set (0.00 sec)

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

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

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

其他函数

  1. 查看当前用户user()
sql 复制代码
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
  1. md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串
    再要我们在讲解https的时候有讲过
sql 复制代码
mysql> select md5('admin');
+----------------------------------+
| md5('admin')                     |
+----------------------------------+
| 21232f297a57a5a743894a0e4a801fc3 |
+----------------------------------+
1 row in set (0.00 sec)
  1. database()显示当前正在使用的数据库
sql 复制代码
mysql> select database();
+------------+
| database() |
+------------+
| test_db    |
+------------+
1 row in set (0.00 sec)
  1. password()函数,MySQL数据库使用该函数对用户加密
    MySQL 8.0版本中已经删除了PASSWORD()函数
sql 复制代码
select password('root');
  +-------------------------------------------+
 | password('root')                         |
 +-------------------------------------------+
 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
 +-------------------------------------------+
  1. ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值
    这个有点向三木操作符
sql 复制代码
mysql> select ifnull(null,'adb');
+--------------------+
| ifnull(null,'adb') |
+--------------------+
| adb                |
+--------------------+
1 row in set (0.00 sec)

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

mysql> select ifnull('def','abd');
+---------------------+
| ifnull('def','abd') |
+---------------------+
| def                 |
+---------------------+
1 row in set (0.00 sec)
相关推荐
mqiqe1 小时前
Python MySQL通过Binlog 获取变更记录 恢复数据
开发语言·python·mysql
工业甲酰苯胺1 小时前
MySQL 主从复制之多线程复制
android·mysql·adb
BestandW1shEs1 小时前
谈谈Mysql的常见基础问题
数据库·mysql
重生之Java开发工程师1 小时前
MySQL中的CAST类型转换函数
数据库·sql·mysql
教练、我想打篮球1 小时前
66 mysql 的 表自增长锁
数据库·mysql
Ljw...1 小时前
表的操作(MySQL)
数据库·mysql·表的操作
少说多做3431 小时前
Android 不同情况下使用 runOnUiThread
android·java
难以触及的高度2 小时前
mysql中between and怎么用
数据库·mysql
Estar.Lee3 小时前
时间操作[计算时间差]免费API接口教程
android·网络·后端·网络协议·tcp/ip