目录
日期函数
current_date
current_time
current_timestamp
date
now
date_add
date_sub
datediff
字符串函数
charset
concat
instr
ucase
lcase
left
length
replace
strcmp
substring
ltrim/rtrim/trim
ltrim
rtrim
trim
数学函数
abs
bin
hex
conv
ceiling
floor
format
rand
mod
其他函数
database
ifnull
md5
password
前面提到过 mysql 是有自己的函数的,下面看一下 mysql 常用的函数
函数名称 | 描述 |
---|---|
current_date() | 当前日期 |
current_time() | 当前时间 |
current_timestamp() | 当前时间戳 |
date(datetime) | 返回datetime参数的日期部分 |
date add(date, interval d value_type) | 在date中添加日期或时间interval后的数值单位可以是: year minute second day |
date sub(date, interval d value type) | 在date中添加日期或时间interval后的数值单位可以是: year minute second day |
datediff(datel, date2) | 两个日期的差,单位是天 |
now() | 当前日期时间 |
上面就是常见的日期函数,下面来看日期函数的使用。
创建一个生日表,里面有 id int 类型并且自增,还有 birthday date 类型:
mysql> create table t1(
-> id int primary key auto_increment,
-> birthday date);
Query OK, 0 rows affected (0.01 sec)
mysql> desc t1;
+----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| birthday | date | YES | | NULL | |
+----------+---------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
创建完成后开始插入数据:
mysql> insert into t1(birthday) values('1990-01-01');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+----+------------+
| id | birthday |
+----+------------+
| 1 | 1990-01-01 |
+----+------------+
1 row in set (0.00 sec)
该日期可以直接手动插入,也可以使用前面的日期类函数:
mysql> insert into t1(birthday) values(current_date());
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+----+------------+
| id | birthday |
+----+------------+
| 1 | 1990-01-01 |
| 2 | 2023-08-28 |
+----+------------+
2 rows in set (0.00 sec)
上面的函数,当前日期表示的就是 XXXX年YY月ZZ天,而时间就是具体时间:
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2023-08-28 |
+----------------+
1 row in set (0.00 sec)
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 20:54:52 |
+----------------+
1 row in set (0.00 sec)
在 mysql 中时间戳并不是遗传数字,而是转化成了具体的时间:
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2023-08-28 20:56:04 |
+---------------------+
1 row in set (0.00 sec)
date 类型的数据不仅可以插入日期的类型,还可以插入具体时间:
mysql> select * from t1;
+----+------------+
| id | birthday |
+----+------------+
| 1 | 1990-01-01 |
| 2 | 2023-08-28 |
| 3 | 2023-08-28 |
+----+------------+
3 rows in set (0.00 sec)
mysql> insert into t1(birthday) values(current_timestamp());
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from t1;
+----+------------+
| id | birthday |
+----+------------+
| 1 | 1990-01-01 |
| 2 | 2023-08-28 |
| 3 | 2023-08-28 |
| 4 | 2023-08-28 |
+----+------------+
4 rows in set (0.00 sec)
date 函数可以返回某一时间的日期:
mysql> select date('1990-01-01 22:50:31');
+-----------------------------+
| date('1990-01-01 22:50:31') |
+-----------------------------+
| 1990-01-01 |
+-----------------------------+
1 row in set (0.00 sec)
可以手动输入,也可以使用其他函数:
mysql> select date(current_date());
+----------------------+
| date(current_date()) |
+----------------------+
| 2023-08-28 |
+----------------------+
1 row in set (0.00 sec)
mysql> select date(current_time());
+----------------------+
| date(current_time()) |
+----------------------+
| 2023-08-28 |
+----------------------+
1 row in set (0.00 sec)
now 就是返回当前时间
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2023-08-28 21:03:43 |
+---------------------+
1 row in set (0.00 sec)
其中 now 也可以使用 date 来截取当前日期:
mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2023-08-28 |
+-------------+
1 row in set (0.00 sec)
日期函数中还有可以计算某一日期加时间的:
mysql> select date_add('2023-01-01', interval 10 day);
+-----------------------------------------+
| date_add('2023-01-01', interval 10 day) |
+-----------------------------------------+
| 2023-01-11 |
+-----------------------------------------+
1 row in set (0.00 sec)
该时间计算除了可以加天数,也可以加年、分、秒。
mysql> select date_add('2023-01-01', interval 10 minute);
+--------------------------------------------+
| date_add('2023-01-01', interval 10 minute) |
+--------------------------------------------+
| 2023-01-01 00:10:00 |
+--------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('2023-01-01', interval 10 second);
+--------------------------------------------+
| date_add('2023-01-01', interval 10 second) |
+--------------------------------------------+
| 2023-01-01 00:00:10 |
+--------------------------------------------+
1 row in set (0.00 sec)
上面是对时间进行加,还可以进行减,而且可以减去年、天、分、秒:
mysql> select date_sub('2023-01-01', interval 10 day);
+-----------------------------------------+
| date_sub('2023-01-01', interval 10 day) |
+-----------------------------------------+
| 2022-12-22 |
+-----------------------------------------+
1 row in set (0.00 sec)
datediff 可以计算时间差:
mysql> select datediff('2023-08-28', '1980-01-01');
+--------------------------------------+
| datediff('2023-08-28', '1980-01-01') |
+--------------------------------------+
| 15945 |
+--------------------------------------+
1 row in set (0.00 sec)
其中里面的参数可以使用函数:
mysql> select datediff(now(), '1980-01-01');
+-------------------------------+
| datediff(now(), '1980-01-01') |
+-------------------------------+
| 15945 |
+-------------------------------+
1 row in set (0.01 sec)
datediff 里面的参数是第一个时间减去第二个时间:
mysql> select datediff('1980-01-01',now());
+------------------------------+
| datediff('1980-01-01',now()) |
+------------------------------+
| -15945 |
+------------------------------+
1 row in set (0.00 sec)
mysql 除了常用到日期函数,还常用到字符串函数。
函数名称 | 描述 |
---|---|
charset(str) | 返回字符串字符集 |
concat(string2 [,...]) | 连接字符串 |
instr(string,substring) | 返回substring在string中出现的位置,没有返回0 |
ucase(string2) | 转换成大写 |
lcase(string2) | 转换成小写 |
left(string2, length) | 从string2中的左边起取length个字符 |
length(string) | string的长度 |
replace(str, search str, replace str) | 在str中用replace_str替换search str |
strcmp(stringl, string2) | 逐字符比较两字符串大小 |
substring(str, position [,length]) | 从str的postion开始,取length个字符 |
ltrim(string) rtrim(string) trim(string) | 去除前空格或后空格 |
该函数用于查看字符编码:
mysql> select charset('abcd');
+-----------------+
| charset('abcd') |
+-----------------+
| utf8 |
+-----------------+
1 row in set (0.00 sec)
我们的mysql 已经配置过了,默认就是 utf8 的编码。其中该函数不仅可以这样查看也可以查看表中的数据:
mysql> select * from employee;
+----+--------------+--------+--------------+----------+
| id | name | gender | dept | sal |
+----+--------------+--------+--------------+----------+
| 1 | 孙悟空 | 男 | 安全部门 | 13000.00 |
| 2 | 玉皇大帝 | 男 | 政治部门 | 8000.00 |
| 3 | 女儿国王 | 女 | 辅助部门 | 5000.00 |
| 4 | 白骨精 | 女 | 辅助部门 | 4500.00 |
| 5 | 猪八戒 | 男 | 安全部门 | 10000.00 |
| 6 | 白龙马 | 男 | 交通部门 | 5500.00 |
| 8 | 观音菩萨 | 女 | 政治部门 | 15000.00 |
+----+--------------+--------+--------------+----------+
7 rows in set (0.00 sec)
mysql> select charset(name) from employee;
+---------------+
| charset(name) |
+---------------+
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
+---------------+
7 rows in set (0.00 sec)
该函数时用于字符串连接,在语言中也有部分这种函数,可能函数名并不一样:
mysql> select concat('a', 'b', 'c');
+-----------------------+
| concat('a', 'b', 'c') |
+-----------------------+
| abc |
+-----------------------+
1 row in set (0.00 sec)
该函数不仅可以连接字符串,也可以将数字也连接起来:
mysql> select concat('a', 'b', 'c', 99999, 0.1234);
+--------------------------------------+
| concat('a', 'b', 'c', 99999, 0.1234) |
+--------------------------------------+
| abc999990.1234 |
+--------------------------------------+
1 row in set (0.00 sec)
可以看一下该函数的实际作用,下面我们可以查看exam_result 表中的学生成绩:
mysql> select concat('名字:', name, ',总分:',chinese+math+english, ',语文成绩:',chinese, '数学成绩:',math, '英语成绩:',english) as 成绩 from exam_result;
+--------------------------------------------------------------------------------------+
| 成绩 |
+--------------------------------------------------------------------------------------+
| 名字:林黛玉,总分:287,语文成绩:98数学成绩:90英语成绩:99 |
| 名字:薛宝钗,总分:266,语文成绩:88数学成绩:90英语成绩:88 |
| 名字:赵姨娘,总分:262,语文成绩:79数学成绩:90英语成绩:93 |
| 名字:小白龙,总分:228,语文成绩:99数学成绩:110英语成绩:19 |
+--------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)
instr(a, b) 该函数查看 b 是否在 a 中,如果有的话,返回b在a中的的起始位置,否则返回0。
mysql> select instr('hello world', 'world');
+-------------------------------+
| instr('hello world', 'world') |
+-------------------------------+
| 7 |
+-------------------------------+
1 row in set (0.00 sec)
该函数将小写转化为大写:
mysql> select ucase('abcd1234ABCD');
+-----------------------+
| ucase('abcd1234ABCD') |
+-----------------------+
| ABCD1234ABCD |
+-----------------------+
1 row in set (0.00 sec)
该函数将大写转化为小写:
mysql> select lcase('abcd1234ABCD');
+-----------------------+
| lcase('abcd1234ABCD') |
+-----------------------+
| abcd1234abcd |
+-----------------------+
1 row in set (0.00 sec)
left(string, length) 该函数从左边开始截取 string 的 length 个字符:
mysql> select left('hello world', 5);
+------------------------+
| left('hello world', 5) |
+------------------------+
| hello |
+------------------------+
1 row in set (0.00 sec)
如果length 大于该字符串原本长度,那么就是全部截取:
mysql> select left('hello world', 25);
+-------------------------+
| left('hello world', 25) |
+-------------------------+
| hello world |
+-------------------------+
1 row in set (0.00 sec)
该函数用于查看字符串的长度,返回的时字节数:
mysql> select length('hello');
+-----------------+
| length('hello') |
+-----------------+
| 5 |
+-----------------+
1 row in set (0.01 sec)
开可以查看汉字:
mysql> select length('中国');
+------------------+
| length('中国') |
+------------------+
| 6 |
+------------------+
1 row in set (0.00 sec)
我们的 mysql utf8,所以每个汉字占3个字节,其中 utf8 还是变长的:
mysql> select length('中国102');
+---------------------+
| length('中国102') |
+---------------------+
| 9 |
+---------------------+
1 row in set (0.00 sec)
replace(string, search, raplace)该函数是替换,将 string 中的 search 的字符串替换成 replace 字符串:
mysql> select replace('中国制造','中国', 'china');
+-------------------------------------------+
| replace('中国制造','中国', 'china') |
+-------------------------------------------+
| china制造 |
+-------------------------------------------+
1 row in set (0.00 sec)
该函数就是字符串比较,若是前面大于后面返回1,相等则返回0,小于则返回-1:
mysql> select strcmp('abc', 'abd');
+----------------------+
| strcmp('abc', 'abd') |
+----------------------+
| -1 |
+----------------------+
1 row in set (0.00 sec)
mysql> select strcmp('abc', 'abc');
+----------------------+
| strcmp('abc', 'abc') |
+----------------------+
| 0 |
+----------------------+
1 row in set (0.00 sec)
mysql> select strcmp('abd', 'abc');
+----------------------+
| strcmp('abd', 'abc') |
+----------------------+
| 1 |
+----------------------+
1 row in set (0.00 sec)
sunstring(string, pos, length)该函数就是对 string 的pos 位置截取 length 长度的字符串:
mysql> select substring('hello world', 7, 5);
+--------------------------------+
| substring('hello world', 7, 5) |
+--------------------------------+
| world |
+--------------------------------+
1 row in set (0.00 sec)
left 只能从左边开始截取,而 substring 可以从任意位置开始。
这几个函数是用来去掉空格的,ltrim 是去掉左边的空格,rtrim 是去掉右边的空格,trim就是去掉两边的空格,这几个函数都不会去掉中间的空格。
mysql> select ltrim(' 7 **** 8 ');
+------------------------------------------+
| ltrim(' 7 **** 8 ') |
+------------------------------------------+
| 7 **** 8 |
+------------------------------------------+
1 row in set (0.00 sec)
mysql> select rtrim(' 7 **** 8 ');
+------------------------------------------+
| rtrim(' 7 **** 8 ') |
+------------------------------------------+
| 7 **** 8 |
+------------------------------------------+
1 row in set (0.00 sec)
实际上这里去掉了,但是这里看的不明显,可以重命名一下:
mysql> select rtrim(' 7 **** 8 ') as rtrim;
+------------------------+
| rtrim |
+------------------------+
| 7 **** 8 |
+------------------------+
1 row in set (0.00 sec)
mysql> select trim(' 7 **** 8 ') as trim;
+--------------+
| trim |
+--------------+
| 7 **** 8 |
+--------------+
1 row in set (0.00 sec)
函数名称 | 描述 |
---|---|
abs (number) | 绝对值函数 |
bin(decimal number) | 十进制转换二进制 |
hex(decimalNumber) | 转换成十六进制 |
conv(number,from base,to base) | 进制转换 |
ceiling(number) | 向上去整 |
floor(number) | 向下去整 |
format(number,decimal places) | 格式化,保留小数位数 |
hex(decimalNumber) | 转换成十六进制 |
rand() | 返回随机浮点数,范围[0.0,1.0) |
mod(number, denominator) | 取模,求余 |
abs 函数用于求绝对值:
mysql> select abs(10);
+---------+
| abs(10) |
+---------+
| 10 |
+---------+
1 row in set (0.00 sec)
mysql> select abs(-10);
+----------+
| abs(-10) |
+----------+
| 10 |
+----------+
1 row in set (0.00 sec)
除了整数,小数也可以:
mysql> select abs(10.11);
+------------+
| abs(10.11) |
+------------+
| 10.11 |
+------------+
1 row in set (0.00 sec)
mysql> select abs(-10.11);
+-------------+
| abs(-10.11) |
+-------------+
| 10.11 |
+-------------+
1 row in set (0.00 sec)
十进制转化为二进制函数:
mysql> select bin(5);
+--------+
| bin(5) |
+--------+
| 101 |
+--------+
1 row in set (0.00 sec)
mysql> select bin(1);
+--------+
| bin(1) |
+--------+
| 1 |
+--------+
1 row in set (0.00 sec)
mysql> select bin(2);
+--------+
| bin(2) |
+--------+
| 10 |
+--------+
1 row in set (0.00 sec)
也可以是负数:
mysql> select bin(-1);
+------------------------------------------------------------------+
| bin(-1) |
+------------------------------------------------------------------+
| 1111111111111111111111111111111111111111111111111111111111111111 |
+------------------------------------------------------------------+
1 row in set (0.00 sec)
小数也可以:
mysql> select bin(1.1);
+----------+
| bin(1.1) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
但是这里的小数是取整了后转化的。
十进制转化为十六进制:
mysql> select hex(15);
+---------+
| hex(15) |
+---------+
| F |
+---------+
1 row in set (0.00 sec)
mysql> select hex(20);
+---------+
| hex(20) |
+---------+
| 14 |
+---------+
1 row in set (0.00 sec)
盎然这个函数也和 bin 函数一样,负数小数都可以。
conv(number, format, base) number 表示哪一个数字,format 表示本来是几进制,base 表示转化为几进制:
mysql> select conv(5, 10, 10);
+-----------------+
| conv(5, 10, 10) |
+-----------------+
| 5 |
+-----------------+
1 row in set (0.00 sec)
mysql> select conv(5, 10, 2);
+----------------+
| conv(5, 10, 2) |
+----------------+
| 101 |
+----------------+
1 row in set (0.00 sec)
mysql> select conv(15, 10, 16);
+------------------+
| conv(15, 10, 16) |
+------------------+
| F |
+------------------+
1 row in set (0.00 sec)
向上取整:
mysql> select ceiling(1.1);
+--------------+
| ceiling(1.1) |
+--------------+
| 2 |
+--------------+
1 row in set (0.00 sec)
mysql> select ceiling(1.5);
+--------------+
| ceiling(1.5) |
+--------------+
| 2 |
+--------------+
1 row in set (0.00 sec)
mysql> select ceiling(1.9);
+--------------+
| ceiling(1.9) |
+--------------+
| 2 |
+--------------+
1 row in set (0.00 sec)
上面是正数向上取整,只要有小数,那么就会变大。
mysql> select ceiling(-1.9);
+---------------+
| ceiling(-1.9) |
+---------------+
| -1 |
+---------------+
1 row in set (0.00 sec)
mysql> select ceiling(-1.5);
+---------------+
| ceiling(-1.5) |
+---------------+
| -1 |
+---------------+
1 row in set (0.00 sec)
mysql> select ceiling(-1.1);
+---------------+
| ceiling(-1.1) |
+---------------+
| -1 |
+---------------+
1 row in set (0.00 sec)
负数向上取整也是变大,对于负数来说绝对值越小则越大。
向下取整:
mysql> select floor(1.1);
+------------+
| floor(1.1) |
+------------+
| 1 |
+------------+
1 row in set (0.00 sec)
mysql> select floor(1.5);
+------------+
| floor(1.5) |
+------------+
| 1 |
+------------+
1 row in set (0.00 sec)
mysql> select floor(1.9);
+------------+
| floor(1.9) |
+------------+
| 1 |
+------------+
1 row in set (0.00 sec)
向下取整也就是变小,不管小数点多大,都会向下变小。
mysql> select floor(-1.1);
+-------------+
| floor(-1.1) |
+-------------+
| -2 |
+-------------+
1 row in set (0.00 sec)
mysql> select floor(-1.5);
+-------------+
| floor(-1.5) |
+-------------+
| -2 |
+-------------+
1 row in set (0.00 sec)
mysql> select floor(-1.9);
+-------------+
| floor(-1.9) |
+-------------+
| -2 |
+-------------+
1 row in set (0.00 sec)
对于负数的变小就是绝对值越来越大,而向下取整就是变小。
format(number, decimal_places) number 表示对哪一个数字进行格式化, decimal_place 表示有几位小数:
mysql> select format('3.1415926', 5);
+------------------------+
| format('3.1415926', 5) |
+------------------------+
| 3.14159 |
+------------------------+
1 row in set (0.01 sec)
mysql> select format('3.1415926', 2);
+------------------------+
| format('3.1415926', 2) |
+------------------------+
| 3.14 |
+------------------------+
1 row in set (0.01 sec)
其中对整数也可以格式化:
mysql> select format('3', 2);
+----------------+
| format('3', 2) |
+----------------+
| 3.00 |
+----------------+
1 row in set (0.00 sec)
该函数就是返回浮点数,范围是0~1:
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.7096299759201985 |
+--------------------+
1 row in set (0.00 sec)
如果想要10以内的浮点数,那么就可以乘10:
mysql> select rand() * 10;
+--------------------+
| rand() * 10 |
+--------------------+
| 5.6270866427822845 |
+--------------------+
1 row in set (0.00 sec)
要100以内的随机整数:
mysql> select format(rand() * 100, 0);
+--------------------------+
| format(rand() * 100, 0) |
+--------------------------+
| 68 |
+--------------------------+
1 row in set (0.00 sec)
mod(number, denominator) 该函数就是对 number 进行取模 :
mysql> select mod(100, 3);
+-------------+
| mod(100, 3) |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
mysql> select mod(2, 3);
+-----------+
| mod(2, 3) |
+-----------+
| 2 |
+-----------+
1 row in set (0.00 sec)
除了正数取模,还可以负数:
mysql> select mod(-10,11);
+-------------+
| mod(-10,11) |
+-------------+
| -10 |
+-------------+
1 row in set (0.00 sec)
mysql> select mod(-10,-11);
+--------------+
| mod(-10,-11) |
+--------------+
| -10 |
+--------------+
1 row in set (0.00 sec)
mysql> select mod(10,-11);
+-------------+
| mod(10,-11) |
+-------------+
| 10 |
+-------------+
1 row in set (0.00 sec)
想要了解负数取模规则的可以自己去查一下,这里就不多说了。
上面是mysql 常用的函数,但是还有一些其他类型的函数也经常使用。
该函数可以用于查看当前在哪一个数据库中
mysql> select database();
+------------+
| database() |
+------------+
| CURD |
+------------+
1 row in set (0.00 sec)
该函数的第一个参数如果为空,那么就返回第二个参数,如果不为空则返回第一个函数:
mysql> select ifnull(null, 1);
+-----------------+
| ifnull(null, 1) |
+-----------------+
| 1 |
+-----------------+
1 row in set (0.00 sec)
mysql> select ifnull(2, 1);
+--------------+
| ifnull(2, 1) |
+--------------+
| 2 |
+--------------+
1 row in set (0.01 sec)
该函数用于加密,并且加密后是32位
mysql> select md5('12345678');
+----------------------------------+
| md5('12345678') |
+----------------------------------+
| 25d55ad283aa400af464c76d713c07ad |
+----------------------------------+
1 row in set (0.00 sec)
mysql> select md5('hello world');
+----------------------------------+
| md5('hello world') |
+----------------------------------+
| 5eb63bbbe01eeed093cb22bb8f5acdc3 |
+----------------------------------+
1 row in set (0.00 sec)
其中如果有一个表如果是保存密码的就可以使用 md5 来加密,保存的密码就是加密后的内容。
password 也是用来加密的,但是加密比 md5 还要严格
mysql> select password('12345678');
+-------------------------------------------+
| password('12345678') |
+-------------------------------------------+
| *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> select password('hello world');
+-------------------------------------------+
| password('hello world') |
+-------------------------------------------+
| *67BECF85308ACF0261750DA1075681EE5C412F05 |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)
其它类型的函数还有一些,想了解更多的可以下去自己查找~