iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >【MySQL】内置函数
  • 550
分享到

【MySQL】内置函数

mysql数据库 2023-08-18 20:08:28 550人浏览 薄情痞子
摘要

文章目录 一、日期函数1.1 日期函数的基本使用1.2 案例11.3 案例2 二、字符串函数三、数学函数四、其他函数 一、日期函数 1.1 日期函数的基本使用 获取当前日期与日期 Mysql> select cu

一、日期函数

在这里插入图片描述

1.1 日期函数的基本使用

  • 获取当前日期与日期
Mysql> select current_date(), current_time();+----------------+----------------+| current_date() | current_time() |+----------------+----------------+| 2023-08-08     | 12:40:57       |+----------------+----------------+1 row in set (0.00 sec)
  • 获取时间戳
mysql> select current_timestamp();+---------------------+| current_timestamp() |+---------------------+| 2023-08-08 12:43:48 |+---------------------+1 row in set (0.00 sec)
  • 获取当前日期时间
mysql> select now();+---------------------+| now()               |+---------------------+| 2023-08-08 12:44:51 |+---------------------+1 row in set (0.00 sec)
  • 在日期的基础上加日期
mysql> select date_add('2020-01-01', interval 12 month) as res;+------------+| res        |+------------+| 2021-01-01 |+------------+1 row in set (0.01 sec)# 当前日期加上7天mysql> select date_add('2023-08-08', interval 7 day) as res;+------------+| res        |+------------+| 2023-08-15 |+------------+1 row in set (0.00 sec)
  • 在日期的基础上减去时间
mysql> select date_sub('2023-08-08', interval 10 day);+-----------------------------------------+| date_sub('2023-08-08', interval 10 day) |+-----------------------------------------+| 2023-07-29  |+-----------------------------------------+1 row in set (0.00 sec)
  • 计算两个日期之间相差多少天
mysql> select datediff('2022-6-15', '2022-1-4');+-----------------------------------+| datediff('2022-6-15', '2022-1-4') |+-----------------------------------+|   162 |+-----------------------------------+1 row in set (0.01 sec)

1.2 案例1

  • 创建生日表
mysql> create table tmp(    -> id bigint primary key auto_increment,    -> birthday date not null    -> );Query OK, 0 rows affected (0.03 sec)
  • 插入日期
mysql> insert into tmp (birthday) values ('1991-1-1');Query OK, 1 row affected (0.01 sec)mysql> insert into tmp (birthday) values ('1999-2-1');Query OK, 1 row affected (0.01 sec)# 可以通过函数插入,插入时间,mysql也会得到日期mysql> insert into tmp (birthday) values (current_date());Query OK, 1 row affected (0.00 sec)# 如果插入时间戳,mysql也会自动截取日期部分mysql> insert into tmp (birthday) values (current_timestamp());Query OK, 1 row affected, 1 warning (0.00 sec)# 方便观察可以带上date函数mysql> insert into tmp (birthday) values (date(current_timestamp()));Query OK, 1 row affected (0.01 sec)mysql> select * from tmp;+----+------------+| id | birthday   |+----+------------+|  1 | 1991-01-01 ||  2 | 1999-02-01 ||  3 | 2023-08-08 ||  4 | 2023-08-08 ||  5 | 2023-08-08 |+----+------------+5 rows in set (0.00 sec)

1.3 案例2

  • 创建一个留言表
mysql> create table msg(    -> id bigint primary key auto_increment,    -> content varchar(100) not null,    -> sendtime datetime    -> );Query OK, 0 rows affected (0.05 sec)mysql> desc msg;+----------+--------------+------+-----+---------+----------------+| Field    | Type         | Null | Key | Default | Extra          |+----------+--------------+------+-----+---------+----------------+| id       | bigint(20)   | NO   | PRI | NULL    | auto_increment || content  | varchar(100) | NO   |     | NULL    |                || sendtime | datetime     | YES  |     | NULL    |                |+----------+--------------+------+-----+---------+----------------+3 rows in set (0.00 sec)
  • 插入数据
mysql> insert into msg (content, sendtime) values ('评论1', now());Query OK, 1 row affected (0.00 sec)mysql> insert into msg (content, sendtime) values ('评论2', now());Query OK, 1 row affected (0.01 sec)mysql> select * from msg;+----+---------+---------------------+| id | content | sendtime            |+----+---------+---------------------+|  1 | 评论1   | 2023-08-08 13:29:51 ||  2 | 评论2   | 2023-08-08 13:29:56 |+----+---------+---------------------+2 rows in set (0.00 sec)
  • 查询在5分钟内发布的评论
mysql> select content,sendtime from msg where sendtime > date_sub(now(),interval 5 minute);+---------+---------------------+| content | sendtime            |+---------+---------------------+| 评论2   | 2023-08-08 13:29:56 |+---------+---------------------+1 row in set (0.00 sec)

二、字符串函数

在这里插入图片描述

  • 获取字符集
mysql> select charset('123');+----------------+| charset('123') |+----------------+| utf8           |+----------------+1 row in set (0.00 sec)mysql> select charset(123);+--------------+| charset(123) |+--------------+| binary       |+--------------+1 row in set (0.00 sec)# 获取sendtime的字符集mysql> select charset(sendtime) from msg;+-------------------+| charset(sendtime) |+-------------------+| binary            || binary            |+-------------------+2 rows in set (0.00 sec)
  • 字符串拼接
mysql> select concat('aa', 'bc', '123');+---------------------------+| concat('aa', 'bc', '123') |+---------------------------+| aabc123                   |+---------------------------+1 row in set (0.00 sec)
  • instr(string,substring)返回substring在string中的位置,不存在返回0
mysql> select instr('abcdef', 'bc');+-----------------------+| instr('abcdef', 'bc') |+-----------------------+|                     2 |+-----------------------+1 row in set (0.00 sec)mysql> select instr('abcdef', 'bcf');+------------------------+| instr('abcdef', 'bcf') |+------------------------+|                      0 |+------------------------+1 row in set (0.00 sec)
  • 将字符串转成大写和小写
mysql> select ucase('AaBbCc123');+--------------------+| ucase('AaBbCc123') |+--------------------+| AABBCC123          |+--------------------+1 row in set (0.00 sec)mysql> select lcase('AaBbCc123');+--------------------+| lcase('AaBbCc123') |+--------------------+| aabbcc123          |+--------------------+1 row in set (0.00 sec)
  • 提取步长个字符

left(string2,length)从string2中,向左截取length个字符

mysql> select left('abc', 2);+----------------+| left('abc', 2) |+----------------+| ab             |+----------------+1 row in set (0.00 sec)

right(string2,length)从string2中,向右截取length个字符

mysql> select right('abc', 2);+-----------------+| right('abc', 2) |+-----------------+| bc              |+-----------------+1 row in set (0.00 sec)
  • 求字符串长度
mysql> select length('abcdef');+------------------+| length('abcdef') |+------------------+|                6 |+------------------+1 row in set (0.00 sec)

看一张成绩表:

mysql> select * from exam_result;+----+--------+---------+------+---------+| id | name   | chinese | math | english |+----+--------+---------+------+---------+|  1 | 张三   |      67 |   98 |      56 ||  2 | 李四   |      87 |   78 |      77 ||  3 | 王五   |      88 |   98 |      90 ||  4 | 赵六   |      82 |   84 |      67 ||  5 | 田七   |      55 |   85 |      45 ||  6 | 孙八   |      70 |   73 |      78 ||  7 | 周九   |      75 |   65 |      30 |+----+--------+---------+------+---------+7 rows in set (0.00 sec)
  • 以“XXX的语文是XXX分,数学XXX分,英语XXX分”的方式显示
mysql> select concat('姓名: ', name, ' 语文: ', chinese, ' 数学: ', math, ' 英语: ', english) msg from exam_result;+-------------------------------------------------+| msg                 |+-------------------------------------------------+| 姓名: 张三 语文: 67 数学: 98 英语: 56           || 姓名: 李四 语文: 87 数学: 78 英语: 77           || 姓名: 王五 语文: 88 数学: 98 英语: 90           || 姓名: 赵六 语文: 82 数学: 84 英语: 67           || 姓名: 田七 语文: 55 数学: 85 英语: 45           || 姓名: 孙八 语文: 70 数学: 73 英语: 78           || 姓名: 周九 语文: 75 数学: 65 英语: 30           |+-------------------------------------------------+7 rows in set (0.00 sec)
  • 将学生表中所有名字中的‘张’的替换成’帅’
mysql> select replace(name, '张', '帅') from exam_result;+-----------------------------+| replace(name, '张', '帅')   |+-----------------------------+| 帅三                        || 李四                        || 王五                        || 赵六                        || 田七                        || 孙八                        || 周九                        |+-----------------------------+7 rows in set (0.00 sec)mysql> select name  from exam_result;+--------+| name   |+--------+| 张三   || 李四   || 王五   || 赵六   || 田七   || 孙八   || 周九   |+--------+7 rows in set (0.00 sec)

注意select配合replace并不会修改原始数据库。

  • 截取学生表中name字段的第二个到第三个字符
mysql> select name,substring(name,2,2) from exam_result;+-----------+---------------------+| name      | substring(name,2,2) |+-----------+---------------------+| 张三      ||| 李四      ||| 王五      ||| 赵六      ||| 田七      ||| 孙八      ||| 周九      ||| 曹孟德    | 孟德                |+-----------+---------------------+8 rows in set (0.00 sec)
  • 去除字符串左右空格
mysql> select ltrim ('  你好  ');+----------------------+| ltrim ('  你好  ')   |+----------------------+| 你好                 |+----------------------+1 row in set (0.01 sec) mysql> select rtrim ('  你好  ');+----------------------+| rtrim ('  你好  ')   |+----------------------+|   你好               |+----------------------+1 row in set (0.01 sec) mysql> select trim('  你好  ');+--------------------+| trim('  你好  ')   |+--------------------+| 你好               |+--------------------+1 row in set (0.00 sec)

三、数学函数

在这里插入图片描述

  • 基本用法
# 取绝对值mysql> select abs(-12);+----------+| abs(-12) |+----------+|       12 |+----------+1 row in set (0.00 sec)# 十进制转二进制mysql> select bin(10);+---------+| bin(10) |+---------+| 1010    |+---------+1 row in set (0.00 sec)# 十进制转16进制mysql> select hex(10);+---------+| hex(10) |+---------+| A       |+---------+1 row in set (0.00 sec)# 任意进制转换(十进制转三进制)mysql> select conv(10,10,3);+---------------+| conv(10,10,3) |+---------------+| 101           |+---------------+1 row in set (0.00 sec)# 保留n位小数mysql> select fORMat(1.23456, 3);+--------------------+| format(1.23456, 3) |+--------------------+| 1.235              |+--------------------+1 row in set (0.00 sec)# 取模mysql> select mod(10, 3);+------------+| mod(10, 3) |+------------+|          1 |+------------+1 row in set (0.00 sec)# 生成随机数(rand范围0~1)mysql> select rand() * 100;+-------------------+| rand() * 100      |+-------------------+| 66.49860438564662 |+-------------------+1 row in set (0.00 sec)

四、其他函数

  • 查询当前用户
mysql> select user();+--------+| user() |+--------+| root@  |+--------+1 row in set (0.00 sec)
  • md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串

创建表:

mysql> create table user(     -> id int unsigned primary key auto_increment,    -> name varchar(20) not null,     -> passWord char(32) not null    -> );Query OK, 0 rows affected (0.04 sec)

插入密码

mysql> insert into user (name, password) values ('张三', 123456);Query OK, 1 row affected (0.00 sec)mysql> select * from user;+----+--------+----------+| id | name   | password |+----+--------+----------+|  1 | 张三   | 123456   |+----+--------+----------+1 row in set (0.00 sec)

在数据库不允许出现明文密码。

需要加密:

mysql> insert into user (name,password) values ('李四',md5('123'));Query OK, 1 row affected (0.00 sec)mysql> select * from user;+----+--------+----------------------------------+| id | name   | password                         |+----+--------+----------------------------------+|  1 | 张三   | 123456                           ||  2 | 李四   | 202cb962ac59075b964b07152d234b70 |+----+--------+----------------------------------+2 rows in set (0.00 sec)

我们进行查找也要通过摘要查找。

mysql> select name,password from user where name='李四' and password=md5('123');+--------+----------------------------------+| name   | password                         |+--------+----------------------------------+| 李四   | 202cb962ac59075b964b07152d234b70 |+--------+----------------------------------+1 row in set (0.00 sec)
  • 也可以使用password函数对密码进行加密。
mysql> select password('123');+-------------------------------------------+| password('123')                           |+-------------------------------------------+| *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |+-------------------------------------------+1 row in set, 1 warning (0.00 sec)
  • 是null返回第二个,不是null返回第一个
mysql> select ifnull(null, 123);+-------------------+| ifnull(null, 123) |+-------------------+|               123 |+-------------------+1 row in set (0.00 sec)mysql> select ifnull(222, 123);+------------------+| ifnull(222, 123) |+------------------+|              222 |+------------------+1 row in set (0.00 sec)

来源地址:https://blog.csdn.net/qq_66314292/article/details/132164381

您可能感兴趣的文档:

--结束END--

本文标题: 【MySQL】内置函数

本文链接: https://www.lsjlt.com/news/374609.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 【MySQL】内置函数
    文章目录 一、日期函数1.1 日期函数的基本使用1.2 案例11.3 案例2 二、字符串函数三、数学函数四、其他函数 一、日期函数 1.1 日期函数的基本使用 获取当前日期与日期 mysql> select cu...
    99+
    2023-08-18
    mysql 数据库
  • 【MySQL--->内置函数】
    文章目录 @[TOC](文章目录) 一、日期函数二、字符串函数三、数学函数四、其他函数 一、日期函数 current_date();当前日期 current_time();当前...
    99+
    2023-10-26
    mysql 数据库
  • MySQL中的内置函数用法
    这篇文章主要讲解了“MySQL中的内置函数用法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“MySQL中的内置函数用法”吧! 用在selec...
    99+
    2024-04-02
  • 【MySQL系列】MySQL内置函数的学习
    「前言」文章内容大致是对MySQL内置函数的学习。 「归属专栏」MySQL 「主页链接」个人主页 「笔者」枫叶先生(fy) 目录 一、MySQL的日期函数二、MySQL的字符串函数三、MySQL的数学函数四、 其它函数...
    99+
    2023-08-28
    mysql 学习 android
  • 内置函数
    目录 内置函数(掌握) 掌握 了解 面向对象知识点 更多内置函数:https://...
    99+
    2023-01-31
    函数
  • mysql中内置的函数有哪些
    这篇文章将为大家详细讲解有关mysql中内置的函数有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。mysql函数MySQL 有很多内置的函数,以下列出了这些函数的说明...
    99+
    2024-04-02
  • golang函数的内置函数
    内置函数是 go 核心的预定义函数,可轻松执行常见任务,例如类型转换、字符串处理和数学运算。具体来说,它们包括:类型转换函数,允许在不同类型之间转换,如 string、int 和 flo...
    99+
    2024-04-21
    golang 内置函数
  • MySQL数据库之内置函数和自定义函数function
    目录1、内置函数1.1、字符串函数1.2、时间函数1.3、数学函数1.4、其他函数2、自定义函数2.1、创建函数2.2、查看函数2.3、调用函数2.4、删除函数2.5、注意事项3、函...
    99+
    2024-04-02
  • PHP array_combine() 函数内置函数
    前言: array_combine() 是 PHP 中的一个内置函数,用于组合两个数组并通过使用一个数组作为键和另一个数组作为值来创建一个新数组。也就是说,一个数组的所有元素将成为新...
    99+
    2024-04-02
  • MySQL内置函数和自定义函数怎么使用
    这篇“MySQL内置函数和自定义函数怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“MySQL内置函数和自定义函数怎么...
    99+
    2023-07-02
  • python内置函数
    什么是内置函数 就是python给你提供的,拿来直接用的函数, 比如print 和 input等等. 截止到python版本3.6.2 python一共提供了68个内置函数. 他们就是python直接提供给我们的,有一些我们已经见过了. ...
    99+
    2023-01-30
    函数 python
  • python 内置函数
    python内置了一系列的常用函数,以便于我们使用python。基本的数据操作基本都是一些数学运算(当然除了加减乘除)、逻辑操作、集合操作、基本IO操作,然后就是对于语言自身的反射操作,还有就是字符串操作。官方文档:https://docs...
    99+
    2023-01-30
    函数 python
  • 自学MySql内置函数知识点总结
    字符串函数 查看字符的ascii码值ascii(str),str是空串时返回0 select ascii('a'); ...
    99+
    2024-04-02
  • python内置函数1
    1.r=compile(s,"<string>","exec")  compile()将字符串编译成python代码2.exec(r)  执行python代码3.eval("8*6") eval("")里面只能执行表达式,执行e...
    99+
    2023-01-31
    函数 python
  • Python的内置函数
    1.什么是内置函数   就是python给你提供的. 拿来直接⽤的函数, 比如print., input等等. 截止到python版本3.6 python一共提供了68个内置函数. 他们就是python直接提供给我们的 Makedo...
    99+
    2023-01-31
    函数 Python
  • MySQL与PHP中的内置函数怎么用
    这篇文章主要介绍MySQL与PHP中的内置函数怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!MySQL 内置函数MySQL 内置函数可以帮助我们更方便的处理表中的数据, 简化操作.数学函数:函数描述ABS()取...
    99+
    2023-06-29
  • PHP 内置函数 var_dump()
    内置的 PHP 函数 var_dump() 可以检索标量和复合变量的信息。 对于每个变量,它将显示变量的数据类型和值。 对于字符串变量,var_dump() 还将检索字符串的长度或大小,如果变量...
    99+
    2024-02-27
  • Python之内置函数
    ''' 内置函数 :     作用域相关(2) :         locals : 返回当前局部作用域内的所有内容         globals : 返回全局作用域内的所有内容     基础数据类型相关(38) :         和数...
    99+
    2023-01-31
    函数 Python
  • 匿名函数,内置函数,闭包
    内容 匿名函数:一句话函数,比较简单的函数。 函数名 = lambda 参数 : 返回值 此函数不是没有名字,他是有名字的,他的名字就是你给其设置的变量,比如func。 func() 函数执行 lambda 是定义匿名函数的关...
    99+
    2023-01-31
    函数
  • MySQL内部函数介绍
    文章目录 字符串函数数学函数日期函数聚合函数系统信息函数格式化函数 字符串函数 1. length(str) 计算字符串的长度。 select length('1+1=wang'),length('1+1=王'); ...
    99+
    2023-08-17
    mysql 数据库 函数
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作