目录
一、日期函数
sql
--获取当前日期(年月日)
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2023-07-28 |
+----------------+
1 row in set (0.02 sec)
--获取当前时间(时分秒)
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 09:48:47 |
+----------------+
1 row in set (0.00 sec)
--获取时间戳(转化为 年月日 时分秒)
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2023-07-28 09:49:33 |
+---------------------+
1 row in set (0.00 sec)
-- 获取当前时间
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2023-07-28 09:50:05 |
+---------------------+
1 row in set (0.00 sec)
-- 从日期时间中提取日期
mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2023-07-28 |
+-------------+
1 row in set (0.00 sec)
-- 当前日期+10天
mysql> select date_add(now(),interval 10 day);
+---------------------------------+
| date_add(now(),interval 10 day) |
+---------------------------------+
| 2023-08-07 10:00:55 |
+---------------------------------+
1 row in set (0.00 sec)
-- 当前日期-10天
mysql> select date_sub(now(),interval 10 day);
+---------------------------------+
| date_sub(now(),interval 10 day) |
+---------------------------------+
| 2023-07-18 10:01:23 |
+---------------------------------+
1 row in set (0.01 sec)
-- 计算两个不同日期相差的天数
mysql> select datediff('2017-10-21','2017-06-01');
+-------------------------------------+
| datediff('2017-10-21','2017-06-01') |
+-------------------------------------+
| 142 |
+-------------------------------------+
1 row in set (0.00 sec)
综合案例1:
sql
mysql> create table tmp( id int primary key auto_increment, birthday date not null);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into tmp(birthday) values('1990-10-10');
Query OK, 1 row affected (0.00 sec)
mysql> insert into tmp(birthday) values('1980-12-21');
Query OK, 1 row affected (0.01 sec)
mysql> select * from tmp;
+----+------------+
| id | birthday |
+----+------------+
| 1 | 1990-10-10 |
| 2 | 1980-12-21 |
+----+------------+
2 rows in set (0.00 sec)
mysql> insert into tmp(birthday) values(current_date());
Query OK, 1 row affected (0.01 sec)
mysql> select * from tmp;
+----+------------+
| id | birthday |
+----+------------+
| 1 | 1990-10-10 |
| 2 | 1980-12-21 |
| 3 | 2023-07-28 |
+----+------------+
3 rows in set (0.00 sec)
mysql> insert into tmp(birthday) values(current_time());
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> select * from tmp;
+----+------------+
| id | birthday |
+----+------------+
| 1 | 1990-10-10 |
| 2 | 1980-12-21 |
| 3 | 2023-07-28 |
| 4 | 2023-07-28 |
+----+------------+
4 rows in set (0.00 sec)
综合案例2:
sql
mysql> create table msg (
-> id int primary key auto_increment,
-> content varchar(30) not null,
-> sendtime datetime
-> );
Query OK, 0 rows affected (0.07 sec)
mysql> insert into msg(content,sendtime)values('好难啊',now());
Query OK, 1 row affected (0.01 sec)
mysql> insert into msg(content,sendtime)values('坚持住',now());
Query OK, 1 row affected (0.01 sec)
mysql> select * from msg;
+----+-----------+---------------------+
| id | content | sendtime |
+----+-----------+---------------------+
| 1 | 好难啊 | 2023-07-28 10:18:07 |
| 2 | 坚持住 | 2023-07-28 10:18:14 |
+----+-----------+---------------------+
2 rows in set (0.00 sec)
mysql> select content,date(sendtime) from msg;
+-----------+----------------+
| content | date(sendtime) |
+-----------+----------------+
| 好难啊 | 2023-07-28 |
| 坚持住 | 2023-07-28 |
+-----------+----------------+
2 rows in set (0.00 sec)
select content,sendtime from msg where sendtime > date_sub(now(),interval 10 minute);
+-----------+---------------------+
| content | sendtime |
+-----------+---------------------+
| 好难啊 | 2023-07-28 10:18:07 |
| 坚持住 | 2023-07-28 10:18:14 |
+-----------+---------------------+
2 rows in set (0.00 sec)
mysql> select content,sendtime from msg where date_add(sendtime,interval 15 minute) > now();
+-----------+---------------------+
| content | sendtime |
+-----------+---------------------+
| 好难啊 | 2023-07-28 10:18:07 |
| 坚持住 | 2023-07-28 10:18:14 |
+-----------+---------------------+
2 rows in set (0.01 sec)
发帖子时间+10分钟 > 当前时间 说明发帖时间在10分钟内
当前时间-10分钟 < 发帖子时间 说明发帖时间在10分钟内
二、字符串函数
sql
--查看字段编码格式
mysql> select charset(name) from exam_result;
+---------------+
| charset(name) |
+---------------+
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
| utf8 |
+---------------+
6 rows in set (0.00 sec)
--字符串拼接
mysql> select concat('姓名:',name,'总分',chinese+math+english) from exam_result;
+------------------------------------------------------+
| concat('姓名:',name,'总分',chinese+math+english) |
+------------------------------------------------------+
| 姓名:唐三藏总分288 |
| 姓名:猪悟能总分364 |
| 姓名:曹孟德总分297 |
| 姓名:刘玄德总分270 |
| 姓名:孙权总分291 |
| 姓名:宋公明总分275 |
+------------------------------------------------------+
6 rows in set (0.00 sec)
--计算字段所占用字节数
mysql> select name,length(name) from exam_result;
+-----------+--------------+
| name | length(name) |
+-----------+--------------+
| 唐三藏 | 9 |
| 猪悟能 | 9 |
| 曹孟德 | 9 |
| 刘玄德 | 9 |
| 孙权 | 6 |
| 宋公明 | 9 |
+-----------+--------------+
6 rows in set (0.00 sec)
--是否在字符串中 在哪个位置
mysql> select instr('aaa1234','1234');
+-------------------------+
| instr('aaa1234','1234') |
+-------------------------+
| 4 |
+-------------------------+
1 row in set (0.00 sec)
--小写转大写
mysql> select ucase('abc123ABC');
+--------------------+
| ucase('abc123ABC') |
+--------------------+
| ABC123ABC |
+--------------------+
1 row in set (0.00 sec)
--大写转小写
mysql> select lcase('abc123ABC');
+--------------------+
| lcase('abc123ABC') |
+--------------------+
| abc123abc |
+--------------------+
1 row in set (0.00 sec)
--从字符串左边拿n个字符
mysql> select left('aaabcd',3);
+------------------+
| left('aaabcd',3) |
+------------------+
| aaa |
+------------------+
1 row in set (0.00 sec)
--从字符串右边拿n个字符
mysql> select right('aaabcd',3);
+-------------------+
| right('aaabcd',3) |
+-------------------+
| bcd |
+-------------------+
1 row in set (0.00 sec)
--把字段中值的替换
mysql> select math,replace(math,'8','6') from exam_result;
+------+-----------------------+
| math | replace(math,'8','6') |
+------+-----------------------+
| 98 | 96 |
| 98 | 96 |
| 90 | 90 |
| 115 | 115 |
| 73 | 73 |
| 95 | 95 |
+------+-----------------------+
6 rows in set (0.00 sec)
--从某个字符开始截取多少个字符
mysql> select name,substring(name,2,2) from exam_result;
+-----------+---------------------+
| name | substring(name,2,2) |
+-----------+---------------------+
| 唐三藏 | 三藏 |
| 猪悟能 | 悟能 |
| 曹孟德 | 孟德 |
| 刘玄德 | 玄德 |
| 孙权 | 权 |
| 宋公明 | 公明 |
+-----------+---------------------+
6 rows in set (0.00 sec)
--以首字母小写的方式显示所有员工的姓名
mysql> select ename,concat(lcase(substring(ename,1,1)),substring(ename,2)) from emp;
+--------+--------------------------------------------------------+
| ename | concat(lcase(substring(ename,1,1)),substring(ename,2)) |
+--------+--------------------------------------------------------+
| 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.00 sec)
--清理两边的空格
mysql> select trim(' 你好 ')as res;
+--------+
| res |
+--------+
| 你好 |
+--------+
1 row in set (0.00 sec)
--清理左边空格
mysql> select ltrim(' 你好 ')as res;
+------------+
| res |
+------------+
| 你好 |
+------------+
1 row in set (0.00 sec)
--清理右边空格
mysql> select rtrim(' 你好 ')as res;
+------------+
| res |
+------------+
| 你好 |
+------------+
三、数学函数
sql
--绝对值
mysql> select abs(10),abs(-10);
+---------+----------+
| abs(10) | abs(-10) |
+---------+----------+
| 10 | 10 |
+---------+----------+
1 row in set (0.00 sec)
--转二进制
mysql> select bin(10),bin(20);
+---------+---------+
| bin(10) | bin(20) |
+---------+---------+
| 1010 | 10100 |
+---------+---------+
1 row in set (0.04 sec)
--转十六进制
mysql> select hex(10),hex(15);
+---------+---------+
| hex(10) | hex(15) |
+---------+---------+
| A | F |
+---------+---------+
1 row in set (0.00 sec)
--一个数字 n进制转m进制
mysql> select conv(10,10,2),conv(10,2,10);
+---------------+---------------+
| conv(10,10,2) | conv(10,2,10) |
+---------------+---------------+
| 1010 | 2 |
+---------------+---------------+
1 row in set (0.00 sec)
--向上取整
mysql> select ceiling(2.31),ceiling(3.14);
+---------------+---------------+
| ceiling(2.31) | ceiling(3.14) |
+---------------+---------------+
| 3 | 4 |
+---------------+---------------+
1 row in set (0.00 sec)
--向下取整
mysql> select floor(2.31),floor(3.14);
+-------------+-------------+
| floor(2.31) | floor(3.14) |
+-------------+-------------+
| 2 | 3 |
+-------------+-------------+
1 row in set (0.00 sec)
--格式化数字 去小数点后几位
mysql> select format(3.1415926,2);
+---------------------+
| format(3.1415926,2) |
+---------------------+
| 3.14 |
+---------------------+
1 row in set (0.00 sec)
--生成随机数
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.6888377207227403 |
+--------------------+
1 row in set (0.00 sec)
mysql> select rand();
+---------------------+
| rand() |
+---------------------+
| 0.17741031961274364 |
+---------------------+
1 row in set (0.00 sec)
mysql> select rand()*100;
+-------------------+
| rand()*100 |
+-------------------+
| 82.05384666291424 |
+-------------------+
1 row in set (0.00 sec)
mysql> select rand()*100;
+------------------+
| rand()*100 |
+------------------+
| 57.0461405041126 |
+------------------+
1 row in set (0.00 sec)
四、其他函数&&OJ
sql
--user() 查询当前用户
mysql> select user();
+--------+
| user() |
+--------+
| zzg@ |
+--------+
1 row in set (0.00 sec)
--md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串
mysql> select md5('admin');
+----------------------------------+
| md5('admin') |
+----------------------------------+
| 21232f297a57a5a743894a0e4a801fc3 |
+----------------------------------+
1 row in set (0.00 sec)
--password()函数,MySQL数据库使用该函数对用户加密
mysql> select password('admin');
+-------------------------------------------+
| password('admin') |
+-------------------------------------------+
| *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)
--database()显示当前正在使用的数据库
mysql> select database();
+------------+
| database() |
+------------+
| scott |
+------------+
1 row in set (0.00 sec)
mysql> select ifnull('abc','123');
+---------------------+
| ifnull('abc','123') |
+---------------------+
| abc |
+---------------------+
1 row in set (0.00 sec)
--ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值
mysql> select ifnull(null,'123');
+--------------------+
| ifnull(null,'123') |
+--------------------+
| 123 |
+--------------------+
1 row in set (0.00 sec)
密码加密
sql
--建表
mysql> create table user(
-> id int primary key auto_increment,
-> password char(32) not null);
Query OK, 0 rows affected (0.06 sec)
--插入数据
mysql> insert into user(password) values(md5('123'));
Query OK, 1 row affected (0.01 sec)
--查看数据
mysql> select * from user;
+----+----------------------------------+
| id | password |
+----+----------------------------------+
| 1 | 202cb962ac59075b964b07152d234b70 |
+----+----------------------------------+
1 row in set (0.00 sec)
--正常找密码找不到
mysql> select * from user where password='123';
Empty set (0.00 sec)
--md5()加密找才可以
mysql> select * from user where password=md5('123');
+----+----------------------------------+
| id | password |
+----+----------------------------------+
| 1 | 202cb962ac59075b964b07152d234b70 |
+----+----------------------------------+
1 row in set (0.00 sec)
--用password()加密更多,mysql自动
mysql> select password('1231');
+-------------------------------------------+
| password('1231') |
+-------------------------------------------+
| *78D02788CBC4FB05D363AC13FDABB60C106B1584 |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)
答案:
sql
select id,length(string)-length(replace(string,',','')) cnt from strings