文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

【MySQL】内置函数

2023-08-18 20:12

关注

在这里插入图片描述

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)
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)
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)
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并不会修改原始数据库。

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)

创建表:

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)
mysql> select password('123');+-------------------------------------------+| password('123')                           |+-------------------------------------------+| *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |+-------------------------------------------+1 row in set, 1 warning (0.00 sec)
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

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯