【Mysql】Mysql内置函数介绍

news/2024/10/18 7:58:07/

00

🌈欢迎来到Python专栏
🙋🏾‍♀️作者介绍:前PLA队员 目前是一名普通本科大三的软件工程专业学生
🌏IP坐标:湖北武汉
🍉 目前技术栈:C/C++、Linux系统编程、计算机网络、数据结构、Mysql、Python(目前在学)
🍇 博客介绍:通过分享学习过程,加深知识点的掌握,也希望通过平台能认识更多同僚,如果觉得文章有帮助,请您动动发财手点点赞,本人水平有限,有不足之处欢迎大家扶正~
🍓 最后送大家一句话共勉:知不足而奋进,望远山而前行。愿大家都能早日进大厂实现财富自由~
————————————————

内置函数介绍

  • 1.日期函数
    • 1.1基本操作
    • 1.2实例操作
  • 2.字符串函数
  • 3.数学函数
  • 4.其他函数

1.日期函数

1.1基本操作

00

  • 获得年月日:
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 |
+------------------------------------+

1.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 | 2017-11-19 |
+----+------------+
mysql> create table msg (
id int primary key auto_increment,
content varchar(30) not null,
sendtime datetime
);
  • 案例-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 | 2017-11-19 14:12:20 |
|  2 | hello2 | 2017-11-19 14:13:21 |
+----+---------+---------------------+
  • 显示所有留言信息,发布日期只显示日期,不用显示时间
select content,date(sendtime) from msg;
  • 请查询在2分钟内发布的帖子
select * from msg where date_add(sendtime, interval 2 minute) > now();
理解:
------------------------------|-----------|-------------|------------------初始时间   now()    初始时间+2min 

2.字符串函数

02

  • 案例:
    获取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;

注意:length函数返回字符串长度,以字节为单位。如果是多字节字符则计算多个字节数;
如果是单字节字符则算作一个字节。比如:字母,数字算作一个字节,中文表示多个字节数
(与字符集编码有关

  • 将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.数学函数

03

绝对值 
select abs(-100.2);
向上取整
select ceiling(23.04);
向下取整
select floor(23.7);
保留2位小数位数(小数四舍五入)
select format(12.3456, 2);
产生随机数
select rand();

4.其他函数

user() 查询当前用户
md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串
database()显示当前正在使用的数据库
password()函数,MySQL数据库使用该函数对用户加密

http://www.ppmy.cn/news/1222744.html

相关文章

ython 的 http.client 和 python-requests 性能差异分析与优化

在Python中,有两个常用的HTTP库,分别是http.client和python-requests。。 社区成员Lukasa提出了一个可能的原因,即python-requests可能没有正确缓存主机名查找。这个问题涉及到了底层的网络操作,因为Python-requests实际上是在ht…

移动机器人路径规划(二)--- 图搜索基础,Dijkstra,A*,JPS

目录 1 图搜索基础 1.1 机器人规划的配置空间 Configuration Space 1.2 图搜索算法的基本概念 1.3 启发式的搜索算法 Heuristic search 2 A* Dijkstra算法 2.1 Dijkstra算法 2.2 A*&&Weighted A*算法 2.3 A* 算法的工程实践中的应用 3 JPS 1 图搜索基础 1.1…

es的使用方法以及概念

Elasticsearch(简称为ES)是一个开源的搜索引擎,它构建在Lucene搜索引擎之上。它提供了一个分布式、多租户的全文搜索引擎,具有强大的实时分析能力。以下是关于Elasticsearch的一些基本概念和使用方法: 基本概念&#…

k8s自定义Endpoint实现内部pod访问外部应用

自定义endpoint实现内部pod访问外部应用 endpoint除了可以暴露pod的IP和端口还可以代理到外部的ip和端口 使用场景 公司业务还还没有完成上云, 一部分云原生的,一部分是实体的 业务上云期间逐步实现上云,保证各个模块之间的解耦性 比如使…

当攻防演练已成常态,企业应该相信西医还是老中医?

在面对疾病时,很多人常常会犹豫不决,不知道应该选择中医还是西医进行治疗。与疾病斗争的过程也是一场“战斗”,需要选择合适的“武器”和策略。有些人认为西医疗效快,能够迅速缓解症状;而另一些人则认为中医治疗更根本…

前台页面从数据库中获取下拉框值

后端&#xff1a;查询所有信息 前台&#xff1a;elementUI <el-select v-model"searchData.stationName" clearable> <el-option :label"item.stationName" :value"item.stationName" v-for"item in stationNameList&quo…

图解分布式事务实现原理(三)

参考 本文参考https://zhuanlan.zhihu.com/p/650791238从零到一搭建 TCC 分布式事务框架&#xff0c;并在小徐的基础上增加个人见解笔记。 项目地址&#xff1a;https://github.com/xiaoxuxiansheng/gotcc 图解分布式事务实现原理&#xff08;一&#xff09;&#xff1a;https…

SpringBoot3自动配置流程及原理、SpringBootApplication注解详解

参考尚硅谷课程: https://www.yuque.com/leifengyang/springboot3/vznmdeb4kgn90vrx https://www.yuque.com/leifengyang/springboot3/lliphvul8b19pqxp 1.自动配置流程及原理 核心流程总结: 1.导入starter&#xff0c;就会导入autoconfigure包 2.autoconfigure 包里面 有一个…