日期函数
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1bb6144ac281a5ad74ec4c67ca331ec6.png)
select current_date();
+----------------+
| current_date() |
+----------------+
| 2017-11-19 |
+----------------+
select current_time();
+----------------+
| current_time() |
+----------------+
| 13:51:21 |
+----------------+
select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2017-11-19 13:51:48 |
+---------------------+
select date_add('2017-10-28', interval 10 day);
+-----------------------------------------+
| date_add('2017-10-28', interval 10 day) |
+-----------------------------------------+
| 2017-11-07 |
+-----------------------------------------+
select date_sub('2017-10-1', interval 2 day);
+---------------------------------------+
| date_sub('2017-10-1', interval 2 day) |
+---------------------------------------+
| 2017-09-29 |
+---------------------------------------+
select datediff('2017-10-10', '2016-9-1');
+------------------------------------+
| datediff('2017-10-10', '2016-9-1') |
+------------------------------------+
| 404 |
+------------------------------------+
create table tmp(
id int primary key auto_increment,
birthday date
);
insert into tmp(birthday) values(current_date());
mysql> select * from tmp;
+----+------------+
| id | birthday |
+----+------------+
| 1 | 2017-11-19 |
+----+------------+
mysql> create table msg (
id int primary key auto_increment,
content varchar(30) not null,
sendtime datetime
);
mysql> insert into msg(content,sendtime) values('hello1', now());
mysql> insert into msg(content,sendtime) values('hello2', now());
mysql> select * from msg;
+----+---------+---------------------+
| id | content | sendtime |
+----+---------+---------------------+
| 1 | hello1 | 2017-11-19 14:12:20 |
| 2 | hello2 | 2017-11-19 14:13:21 |
+----+---------+---------------------+
- 显示所有留言信息,发布日期只显示日期,不用显示时间
select content,date(sendtime) from msg;
select * from msg where date_add(sendtime, interval 2 minute) > now();
理解:
------------------------------|-----------|-------------|------------------
初始时间 now() 初始时间+2min
字符串函数
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3ef002c9a0fda26bf11fcea2ef45a747.png)
select charset(ename) from EMP;
- 要求显示exam_result表中的信息,显示格式:“XXX的语文是XXX分,数学XXX分,英语XXX分”
select concat(name, '的语文是',chinese,'分,数学是',math,'分') as '分数' from student;
select length(name), name from student;
注意:length函数返回字符串长度,以字节为单位。如果是多字节字符则计算多个字节数;
如果是单字节字符则算作一个字节。比如:字母,数字算作一个字节,中文表示多个字节数(与字符集编码有关)
select replace(ename, 'S', '上海') ,ename from EMP;
select substring(ename, 2, 2), ename from EMP;
select concat(lcase(substring(ename, 1, 1)),substring(ename,2)) from EMP;
数学函数
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/544857ab576db4febac3a39aaf6770fc.png)
select abs(-100.2);
select ceiling(23.04);
select floor(23.7);
select format(12.3456, 2);
select rand();
其它函数
select user();
- md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串
select md5('admin')
+----------------------------------+
| md5('admin') |
+----------------------------------+
| 21232f297a57a5a743894a0e4a801fc3 |
+----------------------------------+
select database();
- password()函数,MySQL数据库使用该函数对用户加密
select password('root');
+-------------------------------------------+
| password('root') |
+-------------------------------------------+
| *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-------------------------------------------+
- ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值
select ifnull('abc', '123');
+----------------------+
| ifnull('abc', '123') |
+----------------------+
| abc |
+----------------------+
1 row in set (0.01 sec)
select ifnull(null, '123');
+---------------------+
| ifnull(null, '123') |
+---------------------+
| 123 |
+---------------------+
1 row in set (0.00 sec)