王海鱼 发表于 2024-7-19 13:37:36

[MySQL][内置函数][日期函数][字符串函数][数学函数]详细解说

1.日期函数

1.底子语法

https://i-blog.csdnimg.cn/direct/fb819ce6c99c436d80a94779a6ce2cc3.png


[*]日期时间在MYSQL中是区分开的

[*]日期:年月日
[*]时间:时分秒

[*]获得年月日select current_date();
+----------------+
| current_date() |
+----------------+
| 2024-01-24   |
+----------------+

[*]获得时分秒select current_time();
+----------------+
| current_time() |
+----------------+
| 13:51:21       |
+----------------+

[*]获得时间戳select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2024-01-24 13:51:48 |
+---------------------+

[*]在日期的底子上加日期select date_add('2024-01-14', interval 10 day);
+-----------------------------------------+
| date_add('2024-01-14', interval 10 day) |
+-----------------------------------------+
| 2024-01-24                              |
+-----------------------------------------+

[*]在日期的底子上减去时间select date_sub('2024-01-24', interval 2 day);
+----------------------------------------+
| date_sub('2024-01-24', interval 2 day) |
+----------------------------------------+
| 2024-01-22                           |
+----------------------------------------+

[*]盘算两个日期之间相差多少天+-------------------------------+
| datediff(now(), '1949-10-01') |
+-------------------------------+
|                         27143 |
+-------------------------------+

2.示例1



[*]创建一张表,记载生日,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| 2024-01-24 |
+----+------------+

3.示例2



[*]创建一个留言表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| 2024-01-24 14:12:20 |
| 2| hello2| 2024-01-24 14:13:21 |
+----+---------+---------------------+

[*]表现所有留言信息,发布日期只表现日期,不用表现时间select content,date(sendtime) from msg;

[*]请查询在2分钟内发布的帖子

[*]理解:以下两种写法意思雷同

[*]msg_time > cur_time - 2
[*]msg_time + 2 > cur_time
![]

select * from msg where date_add(sendtime, interval 2 minute) > now();
select * from msg where date_sub(now(), interval 2 minute) < sendtime;

2.字符串函数

1.底子语法

https://i-blog.csdnimg.cn/direct/72678ce7a2224930b02763bf96fad327.png


[*]注意:

[*]length函数返回字符串长度,以字节为单元
[*]如果是多字节字符则盘算多个字节数;如果是单字节字符则算作一个字节

[*]好比:字母,数字算作一个字节,中文表示多个字节数(与字符集编码有关)


2.示例



[*]获取emp表的ename列的字符集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;

[*]将EMP表中所闻名字中有S的替换成’上海’select replace(ename, 'S', '上海'), ename from EMP;

[*]截取EMP表中ename字段的第二个到第三个字符select substring(ename, 2, 2), ename from EMP;

[*]以首字母小写的方式表现所有员工的姓名select concat(lcase(substring(ename, 1, 1)), substring(ename,2)) from EMP;

3.数学函数

1.底子语法

https://i-blog.csdnimg.cn/direct/2253db6662d84bf2a90f83d8eb13747b.png


[*]关于ceiling和floor

[*]C语言中的取整为**0向取证**
[*]ceiling是向+∞取整
[*]floor是向-∞取整

2.示例



[*]绝对值select abs(-100.2);

[*]向上取整select ceiling(23.04);

[*]向下取整select floor(23.7);

[*]保留2位小数位数(小数四舍五入)select format(12.3456, 2);

[*]产生随机数select rand();

4.其他函数



[*]user() 查询当前用户select user();

[*]**md5(str)**对一个字符串举行md5摘要,摘要后得到一个32位字符串select md5('admin')
+----------------------------------+
| md5('admin')                     |
+----------------------------------+
| 21232f297a57a5a743894a0e4a801fc3 |
+----------------------------------+

[*]**database()**表现当前正在使用的数据库select database();

[*]**password()**函数,MySQL数据库使用该函数对用户加密select password('root');
+-------------------------------------------+
| password('root')                        |
+-------------------------------------------+
| *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-------------------------------------------+

[*]ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值

[*]类似C语言中的三目运算符 a?b:c --> (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)


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: [MySQL][内置函数][日期函数][字符串函数][数学函数]详细解说