首页 > 数据库 >【MySQL–07】内置函数

【MySQL–07】内置函数

时间:2023-05-04 22:02:28浏览次数:44  
标签:内置 07 MySQL 2023 set sec mysql select row

【MySQL--07】内置函数

1.函数

1.1日期函数

函数名称 描述
current_data() 当前日期
current_time() 当前时间
current_timestamp() 当前时间戳
date(datetime) 返回datetume参数的日期部分
date_add(date,interval d_value_type) date中添加日期时间 interval后的数值单位可以是: year_minute second day
date_sub(data,interval d_value_type) date中减去日期或时间 interval后的数值单位可以是: year minute second day
datadiff(date1,date2) 两个日期的差,单位是天
now() 当前日期时间
  • 获得年月日:
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2023-05-04     |
+----------------+
1 row in set (0.14 sec)
  • 获得时分秒:
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 17:30:28       |
+----------------+
1 row in set (0.01 sec)
  • 获得时间戳:
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2023-05-04 17:31:29 |
+---------------------+
1 row in set (0.03 sec)
  • 在日期的基础上加日期:
mysql> select date_add('2023-5-1',interval 10 day);
+--------------------------------------+
| date_add('2023-5-1',interval 10 day) |
+--------------------------------------+
| 2023-05-11                           |
+--------------------------------------+
1 row in set (0.01 sec)

--可以重命名
mysql> select date_add('2023-5-1',interval 10 day) as result;
+------------+
| result     |
+------------+
| 2023-05-11 |
+------------+
1 row in set (0.01 sec)

--as可以省略
mysql> select date_add('2023-5-1',interval 10 day) result;
+------------+
| result     |
+------------+
| 2023-05-11 |
+------------+
1 row in set (0.00 sec)
  • 在日期的基础上减日期:
mysql> select date_sub('2023-5-1',interval 10 day);
+--------------------------------------+
| date_sub('2023-5-1',interval 10 day) |
+--------------------------------------+
| 2023-04-21                           |
+--------------------------------------+
1 row in set (0.00 sec)

mysql> select date_sub('2023-5-1',interval 10 day) as result;
+------------+
| result     |
+------------+
| 2023-04-21 |
+------------+
1 row in set (0.00 sec)

mysql> select date_sub('2023-5-1',interval 10 day) result;
+------------+
| result     |
+------------+
| 2023-04-21 |
+------------+
1 row in set (0.00 sec)
  • 计算两个日期之间相差多少天
mysql> select datediff('2023-5-1','2023-6-1');
+---------------------------------+
| datediff('2023-5-1','2023-6-1') |
+---------------------------------+
|                             -31 |
+---------------------------------+
1 row in set (0.03 sec)

mysql> select datediff('2023-6-1','2023-5-1');
+---------------------------------+
| datediff('2023-6-1','2023-5-1') |
+---------------------------------+
|                              31 |
+---------------------------------+
1 row in set (0.00 sec)

我们可能够发现是前面的日期减去后面的日期的时间差

1.1.1案例1

  • 创建一张表,记录生日
mysql> create table tmp(
    -> id int primary key auto_increment,
    -> birthday date);
Query OK, 0 rows affected (0.29 sec)
  • 添加当前日期
mysql> insert into tmp(birthday) values (current_date());
Query OK, 1 row affected (0.04 sec)

mysql> select * from tmp;
+----+------------+
| id | birthday   |
+----+------------+
|  1 | 2023-05-04 |
+----+------------+
1 row in set (0.00 sec)

1.1.2案例2

  • 创建一个留言表
mysql> create table msg(
    -> id int primary key auto_increment,
    -> content varchar(20) not null,
    -> sendtime datetime);
Query OK, 0 rows affected (0.23 sec)
  • 插入数据
mysql> insert into msg(content,sendtime) values ('hello1',now());
Query OK, 1 row affected (0.04 sec)

mysql> insert into msg(content,sendtime) values ('hello2',now());
Query OK, 1 row affected (0.05 sec)

mysql> select * from msg;
+----+---------+---------------------+
| id | content | sendtime            |
+----+---------+---------------------+
|  1 | hello1  | 2023-05-04 17:41:42 |
|  2 | hello2  | 2023-05-04 17:41:47 |
+----+---------+---------------------+
2 rows in set (0.01 sec)
  • 显示所有留言信息,发布日期只显示日期,不用显示时间

select content,date(sendtime) from msg;

mysql> select content,date(sendtime) from msg;
+---------+----------------+
| content | date(sendtime) |
+---------+----------------+
| hello1  | 2023-05-04     |
| hello2  | 2023-05-04     |
+---------+----------------+
2 rows in set (0.01 sec)
  • 请查询在2分钟内发布的贴子
select * from msg where date_add(sendtime,interval 2 minute) > now();
理解:

------------------------------|-----------|-------------|------------------

                           初始时间     now()       初始时间+2min           

1.2字符串函数

charset(str) 返回字符串字符集
concat(string2 [,...]) 连接字符串
instr(string,substring) 返回substring在string中出现的位置,没有返回0
ucase(string2) 转换成大写
lcase(string2) 转换成小写
left(string2,length) string2中的左边起取length个字符
length(string) string的长度
replace(str,search_str,replace_str) str中用replace_str替换search_str
strcmp(string1,string2) 逐字符比较两字符的大小
substring(str,position [,length]) strpostion开始,取length个字符
ltrim(string) rtrim(string) trim(string) 去除前空格或后空格

案例:

创建表

mysql> create table emp( 
	-> eid int primary key auto_increment, 
	-> ename varchar(20), 
	-> chinese int,
    -> math int,
    -> english int);
Query OK, 0 rows affected (0.23 sec)

插入数据若干:

mysql> insert into emp values(null,'孙悟空',80,90,77);
Query OK, 1 row affected (0.02 sec)

mysql> insert into emp values(null,'沙和尚',62,67,72);
Query OK, 1 row affected (0.03 sec)
  • 获取emp表的ename列的字符集
mysql> select charset(ename) from emp;
+----------------+
| charset(ename) |
+----------------+
| utf8           |
| utf8           |
+----------------+
2 rows in set (0.01 sec)
  • 要求显示emp表中的信息,显示格式为:xxx的语文成绩是xxx分,数学xxx分,英语xxx分
mysql> select concat(ename,'的语文成绩是',chinese,'分,数学',math,'分,英语',english,'分') as '分数' from emp;
+----------------------------------------------------------+
| 分数                                                     |
+----------------------------------------------------------+
| 孙悟空的语文成绩是80分,数学90分,英语77分                 |
| 沙和尚的语文成绩是62分,数学67分,英语72分                 |
+----------------------------------------------------------+
2 rows in set (0.01 sec)
  • 求学生表中学生姓名占用的字节数
mysql> select length(ename),ename from emp;
+---------------+-----------+
| length(ename) | ename     |
+---------------+-----------+
|             9 | 孙悟空    |
|             9 | 沙和尚    |
+---------------+-----------+
2 rows in set (0.01 sec)

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

  • 讲emp表中所有名字中有‘孙’的替换成'sun'
mysql> select replace(ename,'孙','sun'),ename from emp;
+----------------------------+-----------+
| replace(ename,'孙','sun')  | ename     |
+----------------------------+-----------+
| sun悟空                    | 孙悟空    |
| 沙和尚                     | 沙和尚    |
+----------------------------+-----------+
2 rows in set (0.01 sec)
  • 截取emp表中ename字段的第二个到第三个字符
mysql> select substring(ename,2,2),ename from emp;
+----------------------+-----------+
| substring(ename,2,2) | ename     |
+----------------------+-----------+
| 悟空                 | 孙悟空    |
| 和尚                 | 沙和尚    |
+----------------------+-----------+
2 rows in set (0.01 sec)

注意:这里一个汉字算一个字符,和一个字符占几个字节无关。

  • 以首字母小写的方式显示所有员工的姓名
mysql> select * from emp;
+-----+-----------+---------+------+---------+
| eid | ename     | chinese | math | english |
+-----+-----------+---------+------+---------+
|   1 | 孙悟空    |      80 |   90 |      77 |
|   2 | 沙和尚    |      62 |   67 |      72 |
|   3 | Lisa      |      70 |   80 |      90 |
|   4 | Jack      |      70 |   80 |      90 |
|   5 | Pony      |      70 |   80 |      90 |
|   6 | Tony      |      70 |   80 |      90 |
+-----+-----------+---------+------+---------+
6 rows in set (0.00 sec)

mysql> select concat(lcase(substring(ename, 1, 1)),substring(ename,2)) from emp;
+----------------------------------------------------------+
| concat(lcase(substring(ename, 1, 1)),substring(ename,2)) |
+----------------------------------------------------------+
| 孙悟空                                                   |
| 沙和尚                                                   |
| lisa                                                     |
| jack                                                     |
| pony                                                     |
| tony                                                     |
+----------------------------------------------------------+
6 rows in set (0.00 sec)

1.3数字函数

函数名称 描述
abs(number) 绝对值函数
bin(decimal_number) 十进制转换二进制
hex(decimalNumber) 转换成十六进制
conv(number,from_base,to_base) 进制转换
ceiling(number) 向上取整
floor(number) 向下取整
format(number,decimal_places) 格式化,保留小数位数
rand() 返回随机浮点数,范围[0.0,1.0)
mod(number,denominator) 取模,求余
  • 绝对值
mysql> select abs(-100.2);
+-------------+
| abs(-100.2) |
+-------------+
|       100.2 |
+-------------+
1 row in set (0.01 sec)
  • 向上取整
mysql> select ceiling(23.4);
+---------------+
| ceiling(23.4) |
+---------------+
|            24 |
+---------------+
1 row in set (0.00 sec)

mysql> select ceiling(-23.4);
+----------------+
| ceiling(-23.4) |
+----------------+
|            -23 |
+----------------+
1 row in set (0.00 sec)
  • 向下取整
mysql> select floor(23.4);
+-------------+
| floor(23.4) |
+-------------+
|          23 |
+-------------+
1 row in set (0.00 sec)
mysql> select floor(-23.4);
+--------------+
| floor(-23.4) |
+--------------+
|          -24 |
+--------------+
1 row in set (0.00 sec)
  • 保留2位小数位数(小数四舍五入)
mysql> select format(12.3456789,2);
+----------------------+
| format(12.3456789,2) |
+----------------------+
| 12.35                |
+----------------------+
1 row in set (0.00 sec)
  • 产生随机数
mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.5188169232744881 |
+--------------------+
1 row in set (0.00 sec)

1.4其他函数

  • user()查询当前用户
mysql> select user();
+--------+
| user() |
+--------+
| Lxy@   |
+--------+
1 row in set (0.00 sec)
  • md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串
mysql> select md5('123456');
+----------------------------------+
| md5('123456')                    |
+----------------------------------+
| e10adc3949ba59abbe56e057f20f883e |
+----------------------------------+
1 row in set (0.00 sec)
  • database()显示当前正在使用的数据库
mysql> select database();
+----------------+
| database()     |
+----------------+
| 104_db_lesson7 |
+----------------+
1 row in set (0.00 sec)
  • password()函数,MySQL数据库使用该函数对用户加密
mysql> select password('Lxy');
+-------------------------------------------+
| password('Lxy')                           |
+-------------------------------------------+
| *48708BC40FD52498209A7778EDA8CB7BC76F8710 |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)
  • ifnull(val1,val2)如果val1为null,返回val2,否则返回val1的值
mysql> select ifnull('abc','123');
+---------------------+
| ifnull('abc','123') |
+---------------------+
| abc                 |
+---------------------+
1 row in set (0.01 sec)

mysql> select ifnull(null,'123');
+--------------------+
| ifnull(null,'123') |
+--------------------+
| 123                |
+--------------------+
1 row in set (0.00 sec)

(本篇完)

标签:内置,07,MySQL,2023,set,sec,mysql,select,row
From: https://blog.51cto.com/xingyuli/6244593

相关文章

  • 【必知必会的MySQL知识】④DCL语言
    转载:https://www.cnblogs.com/xiezhr/p/17343320.html 目录一、概述二、授权2.1语法格式2.2语法说明2.3权限类型2.4权限级别三、回收权限3.1语法格式3.2语法说明3.3注意事项四、实践操作 一、概述数据控制语言,用来定义访问权限和安全级别......
  • Django内置序列化组件(drf前身)和批量操作数据加上分页器
    目录一、django内置序列化组件(drf前身)这里的内置序列化组件,其实就是实现将后端数据,存放到字典中或是存放到列表中有序输出。方式一这里是我们用JsonResponse模块自己实现的代码'''前后端分离的项目视图函数只需要返回json格式的数据即可'''fromapp01importmodelsfr......
  • mysql
    mysql目录mysql数据库分类视图触发器事务存储过程函数数据库分类'''市面上有很多的数据库,但是,大致分类两大类'''1.关系型数据库 MySQL、Oracle、SqlServer、PostgreSQL、sqlite、MariaDB、db2、accessMySQL:开源免费的 Oracle:不开源,收费MariaDB:开源的2.非关系......
  • 【必知必会的MySQL知识】mysql5.7安装教程
    1.下载mysql下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads下载zip免安装版,可以省去很多事2.将下载的安装文件解压放到磁盘中3.在mysql解压缩包根目录下创建my.ini文件(mysql主配置文件)并创建data目录(用户初始化数据库文件目录)my.ini文件内容如下[mys......
  • Linux 安装MySql
    Linux安装MySql1.下载mysqlhttps://downloads.mysql.com/archives/community/下载之后使用文件传输工具传输到你的服务器上 2.安装mysql1.创建mysql文件夹mkdir/soft/mysql创建文件夹cd/soft/mysql进入 2.解压文件夹在该目录下再创建一个文件夹,并且将安装......
  • 【解决方案】MySQL死锁解决案例
    说明:该场景为商品库存操作更新MySQL时发生的数据库死锁,如果你没有做过库存系统经验,也可以把方案应用到(优惠券库存or活动库存or抽奖礼品库存等等),只要符合同时更新多条记录时均可。背景:库存系统组合品是由多个单品组合而成,更新数据库时先组装数据再批量更新多个sku。组合品关系......
  • rpm升级mysql小版本
    记录一下升级mysql小版本遇到坑的过程,由于数据量比较多,因此备份时最大的几个表没有备份(备份了数据文件.idb文件),但是有一个表没有备份到,导致后面浪费很久的时间恢复数据正确的升级命令如下:rpm-Uvhmysql-community-common-5.7.41-1.el7.x86_64.rpm rpm-Uvhmysql-community-li......
  • MySQL 8.0中InnoDB buffer pool size进度更透明
    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。GreatSQL是MySQL的国产分支版本,使用上与MySQL一致。作者:Yejinrong/叶金荣文章来源:GreatSQL社区原创MySQL8.0upupup~从MySQL5.7开始,支持在线动态调整innodbbufferpool,并为此新增了一个状态变......
  • MySQL(十八)MySQL事务(一):事务的概述与使用
    MySQL事务(一):事务的概述与使用1数据库事务概述​ 事务是数据库区别于文件系统的重要特性之一,当我们有了事务就会让数据库始终保持一致性,同时我们还能通过事务的机制恢复到某个时间点,这样可以保证已提交到数据库的修改不会因为系统崩溃而丢失。1.1存储引擎支持情况​ show......
  • 5、MySQL的SQL语言、数据库管理、数据类型及DQL的单、多表查询
    进入mysql后,使用help列出的是客户端的命令,使用helpcontents列出服务端命令SQL语句分类(DDLDMLDQL要记住)前三个重要(DDL、DML、DQL、DCL、TCL)DDL:DataDefinationLanguage数据定义语言CREATE,DROP,ALTER(对数据库、表、视图、索引进行创建、删除和更改的工具ALTER改格式)......