MySQL内置函数
- 1 .日期函数
- 2.字符串函数
- 3.数学函数
- 4.其它函数
- 5.综合练习题
1 .日期函数
current_date() 当前日期
- 获得年月日
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2022-12-10 |
+----------------+
1 row in set (0.00 sec)
current_time() 当前时间
- 获得时分秒:
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 15:46:05 |
+----------------+
1 row in set (0.00 sec)
current_timestamp() 当前时间戳
- 获得时间戳:
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2022-12-10 15:46:46 |
+---------------------+
1 row in set (0.00 sec)
date(datetime) 返回datetime参数的日期部分
date_add(date,interval d_value_type) 在date中添加日期或时间
interval 后的数值单位可以是:year minute second day
mysql> select date_add('2022-10-28', interval 10 day);
+-----------------------------------------+
| date_add('2022-10-28', interval 10 day) |
+-----------------------------------------+
| 2022-11-07 |
+-----------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('2022-10-28', interval 10 minute);
+--------------------------------------------+
| date_add('2022-10-28', interval 10 minute) |
+--------------------------------------------+
| 2022-10-28 00:10:00 |
+--------------------------------------------+
1 row in set (0.00 sec)
date_sub(date,interval d_value_type) 在date中减去日期或时间
interval 后的数值单位可以是:year minute second day
mysql> select date_sub('2022-10-28', interval 10 day);
+-----------------------------------------+
| date_sub('2022-10-28', interval 10 day) |
+-----------------------------------------+
| 2022-10-18 |
+-----------------------------------------+
1 row in set (0.00 sec)
mysql> select date_sub('2022-10-28', interval 10 minute);
+--------------------------------------------+
| date_sub('2022-10-28', interval 10 minute) |
+--------------------------------------------+
| 2022-10-27 23:50:00 |
+--------------------------------------------+
1 row in set (0.00 sec)
datediff(date1,date2) 两个日期的差,单位是天
- 计算两个日期之间相差多少天:
mysql> select datediff('2023-12-24', '2022-12-10');
+--------------------------------------+
| datediff('2023-12-24', '2022-12-10') |
+--------------------------------------+
| 379 |
+--------------------------------------+
1 row in set (0.00 sec)
ps:24考研倒计时379天
now() 当前日期时间
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2022-12-10 16:02:03 |
+---------------------+
1 row in set (0.00 sec)
案例:
- 创建一个留言表
mysql> create table msg(
-> id int primary key auto_increment,
-> comtent varchar(30) not null,
-> sendtime datetime
-> );
Query OK, 0 rows affected (0.03 sec)
- 插入数据
mysql> insert into msg(comtent,sendtime) values ('hello',now());
Query OK, 1 row affected (0.01 sec)
mysql> insert into msg(comtent,sendtime) values ('hi',now());
Query OK, 1 row affected (0.00 sec)
mysql> select * from msg;
+----+---------+---------------------+
| id | comtent | sendtime |
+----+---------+---------------------+
| 1 | hello | 2022-12-10 16:08:11 |
| 2 | hi | 2022-12-10 16:08:25 |
+----+---------+---------------------+
2 rows in set (0.00 sec)
- 显示所有留言信息,发布日期只显示日期,不用显示时间
mysql> select comtent,date(sendtime) from msg;
+---------+----------------+
| comtent | date(sendtime) |
+---------+----------------+
| hello | 2022-12-10 |
| hi | 2022-12-10 |
+---------+----------------+
2 rows in set (0.00 sec)
- 请查询在2分钟内发布的帖子
mysql> select * from msg where date_add(sendtime, interval 2 minute) > now();
Empty set (0.01 sec)
mysql> select * from msg where date_add(sendtime, interval 5 minute) > now();
+----+---------+---------------------+
| id | comtent | sendtime |
+----+---------+---------------------+
| 1 | hello | 2022-12-10 16:08:11 |
| 2 | hi | 2022-12-10 16:08:25 |
+----+---------+---------------------+
2 rows in set (0.00 sec)
可以看到,俩分钟内没有发布的贴子,五分钟内有两条。
2.字符串函数
案例:
- chaeset(str)
mysql> select charset('张三');
+-------------------+
| charset('张三') |
+-------------------+
| utf8 |
+-------------------+
1 row in set (0.00 sec)
mysql> select charset(111);
+--------------+
| charset(111) |
+--------------+
| binary |
+--------------+
1 row in set (0.00 sec)
- 要求显示exam_result表中的信息,显示格式:“XXX的语文是XXX分,数学XXX分”
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孙悟空 | 87 | 80 | 77 |
| 3 | 猪悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 70 | 90 | 67 |
| 5 | 刘玄德 | 55 | 115 | 45 |
| 6 | 孙权 | 70 | 73 | 78 |
| 7 | 宋公明 | 75 | 95 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
mysql> select concat(name, '的语文是',chinese,'分,数学是',math,'分') as '分数' from exam_result;
+----------------------------------------------+
| 分数 |
+----------------------------------------------+
| 唐三藏的语文是67分,数学是98分 |
| 孙悟空的语文是87分,数学是80分 |
| 猪悟能的语文是88分,数学是98分 |
| 曹孟德的语文是70分,数学是90分 |
| 刘玄德的语文是55分,数学是115分 |
| 孙权的语文是70分,数学是73分 |
| 宋公明的语文是75分,数学是95分 |
+----------------------------------------------+
7 rows in set (0.00 sec)
- 求exam_result表中人物姓名占用的字节数
mysql> select name,length(name) from exam_result;
+-----------+--------------+
| name | length(name) |
+-----------+--------------+
| 唐三藏 | 9 |
| 孙悟空 | 9 |
| 猪悟能 | 9 |
| 曹孟德 | 9 |
| 刘玄德 | 9 |
| 孙权 | 6 |
| 宋公明 | 9 |
+-----------+--------------+
7 rows in set (0.00 sec)
注意:length函数返回字符串长度,以字节为单位。如果是多字节字符则计算多个字节数;如果是单字节字符则算作一个字节。
比如:字母,数组算作一个字节,中文表示多个字节数(与字符集编码有关)
中文汉字一个字符占3个字节,因此例如 '唐三藏’字节数为9.
- 将exam_result表中数学成绩为115的成绩替换成 ’牛逼‘
mysql> select name,math,replace(math,115,'牛逼') from exam_result;
+-----------+------+----------------------------+
| name | math | replace(math,115,'牛逼') |
+-----------+------+----------------------------+
| 唐三藏 | 98 | 98 |
| 孙悟空 | 80 | 80 |
| 猪悟能 | 98 | 98 |
| 曹孟德 | 90 | 90 |
| 刘玄德 | 115 | 牛逼 |
| 孙权 | 73 | 73 |
| 宋公明 | 95 | 95 |
+-----------+------+----------------------------+
7 rows in set (0.00 sec)
- 截取exam_result表中name字段的第二个到第三个字符
mysql> select name,substring(name,2,3) from exam_result;
+-----------+---------------------+
| name | substring(name,2,3) |
+-----------+---------------------+
| 唐三藏 | 三藏 |
| 孙悟空 | 悟空 |
| 猪悟能 | 悟能 |
| 曹孟德 | 孟德 |
| 刘玄德 | 玄德 |
| 孙权 | 权 |
| 宋公明 | 公明 |
+-----------+---------------------+
7 rows in set (0.00 sec)
- 大小写转换
mysql> select ucase('1234Asd');
+------------------+
| ucase('1234Asd') |
+------------------+
| 1234ASD |
+------------------+
1 row in set (0.00 sec)
mysql> select lcase('1234Asd');
+------------------+
| lcase('1234Asd') |
+------------------+
| 1234asd |
+------------------+
1 row in set (0.00 sec)
- 查看字符@在字符串123asd456zxc@qq.com中首次出现的位置
mysql> select instr('123asd456zxc@qq.com','@');
+----------------------------------+
| instr('123asd456zxc@qq.com','@') |
+----------------------------------+
| 13 |
+----------------------------------+
1 row in set (0.00 sec)
- 去除左右空格
mysql> select ' aaa ' as res;
+------------+
| res |
+------------+
| aaa |
+------------+
1 row in set (0.00 sec)
mysql> select ltrim(' aaa ') as res;
+--------+
| res |
+--------+
| aaa |
+--------+
1 row in set (0.00 sec)
mysql> select rtrim(' aaa ') as res;
+---------+
| res |
+---------+
| aaa |
+---------+
1 row in set (0.00 sec)
mysql> select trim(' aaa ') as res;
+------+
| res |
+------+
| aaa |
+------+
1 row in set (0.00 sec)
3.数学函数
- 绝对值
mysql> select abs(-250.55);
+--------------+
| abs(-250.55) |
+--------------+
| 250.55 |
+--------------+
1 row in set (0.00 sec)
- 向上取整
mysql> select ceiling(23.04);
+----------------+
| ceiling(23.04) |
+----------------+
| 24 |
+----------------+
1 row in set (0.00 sec)
mysql> select ceiling(-23.04);
+-----------------+
| ceiling(-23.04) |
+-----------------+
| -23 |
+-----------------+
1 row in set (0.00 sec)
- 向下取整
mysql> select floor(23.7);
+-------------+
| floor(23.7) |
+-------------+
| 23 |
+-------------+
1 row in set (0.01 sec)
mysql> select floor(-23.7);
+--------------+
| floor(-23.7) |
+--------------+
| -24 |
+--------------+
1 row in set (0.00 sec)
- 保留2位小数位数(小数四舍五入)
mysql> select format(12.3456, 2);
+--------------------+
| format(12.3456, 2) |
+--------------------+
| 12.35 |
+--------------------+
1 row in set (0.00 sec)
- 产生随机数
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.8591073694258066 |
+--------------------+
1 row in set (0.00 sec)
mysql> select rand()*10;
+-------------------+
| rand()*10 |
+-------------------+
| 8.073119305142312 |
+-------------------+
1 row in set (0.00 sec)
mysql> select format(rand()*10,0);
+---------------------+
| format(rand()*10,0) |
+---------------------+
| 5 |
+---------------------+
1 row in set (0.01 sec)
- 将255十进制转换为二进制
mysql> select conv(255,10,2);
+----------------+
| conv(255,10,2) |
+----------------+
| 11111111 |
+----------------+
1 row in set (0.00 sec)
4.其它函数
- user() 查询当前用户
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
- md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串
mysql> select md5('hello');
+----------------------------------+
| md5('hello') |
+----------------------------------+
| 5d41402abc4b2a76b9719d911017c592 |
+----------------------------------+
1 row in set (0.00 sec)
md5简介
md5的全称是md5信息摘要算法(英文:MD5 Message-Digest Algorithm ),一种被广泛使用的密码散列函数,可以产生一个128位(16字节,1字节8位)的散列值(常见的是用32位的16进制表示,比如:0caa3b23b8da53f9e4e041d95dc8fa2c),用于确保信息传输的完整一致。md5原理
md5将整个文件当做一个大文本信息,通过不可逆的字符串变换算法,产生一个唯一的MD5信息摘要。文件的md5类似于人的指纹,在世界上是独立无二的,如果任何人对文件做了任何改动,其md5的值也就是对应的“数字指纹”都会发生变化。md5的用途
密码的加密存储、数字签名、文件完整性验证、文件上传…md5与对称和非对称加密算法不同,这两种密码是防止信息被窃取,而摘要算法的目标是用于证明原文的完整性。
- database()显示当前正在使用的数据库
mysql> select database();
+------------+
| database() |
+------------+
| test2 |
+------------+
1 row in set (0.00 sec)
- ifnull(val1, val2)
如果val1为null,返回val2,否则返回val1的值
mysql> select ifnull(111,222);
+-----------------+
| ifnull(111,222) |
+-----------------+
| 111 |
+-----------------+
1 row in set (0.00 sec)
mysql> select ifnull(null,222);
+------------------+
| ifnull(null,222) |
+------------------+
| 222 |
+------------------+
1 row in set (0.00 sec)
mysql> select ifnull(null,null);
+-------------------+
| ifnull(null,null) |
+-------------------+
| NULL |
+-------------------+
1 row in set (0.00 sec)
5.综合练习题
查找字符串中逗号出现的次数
描述:
现有strings表如下:
- id指序列号;
- string列中存放的是字符串,且字符串中仅包含数字、字母和逗号类型的字符。
id | string |
---|---|
1 | 10,A,B,C,D |
2 | A,B,C,D,E,F |
3 | A,11,B,C,D,E,G |
请你统计每个字符串中逗号出现的次数cnt。
以上例子的输出结果如下:
id | cnt |
---|---|
1 | 4 |
2 | 5 |
3 | 6 |
思路:
统计字符串中逗号出现的次数,我们可以将字符串里的逗号用空串替换,然后用原始字符串长度减去替换后字符串长度,即为逗号数量
解答:
select id,length(string)-length(replace(string,',','')) as cnt from strings;
the end~