目录
一、日期函数
1、函数用法
1.1current_date()、current_time()、current_timestamp()、now()
1.2date(datetime)
1.3date_add(date,interval d_value_type)、date_sub(date,interval d_value_type)、datediff(date1,date2)
2、在表中date字段插入时间、时间戳,MySQL会取得其中的日期部分
3、在表中datetime字段插入时间、时间戳,MySQL会取得其中的日期+时间部分
4、查询在3分钟内发布的帖子
二、字符串函数
1、函数用法
1.1charset(str)、concat(string2 [, ... ])、instr(string,substring)
1.2ucase(string2)、lcase(string2)
1.3left(string2,length)、right(string2,length)
1.4length(string)
1.5获取msg表的content列的字符集
1.6显示exam_result表中的信息,显示格式:“XXX的语文是XXX分,数学XXX分,英语XXX分”
1.7求学生表中学生姓名各自占用的字节数
1.8将学生表中所有名字中的‘张’的替换成'王五'
1.9截取学生表中name字段的第二个到第三个字符
1.10以首字母小写的方式显示所有员工的姓名
1.11删除左右空格
三、数学函数
1、函数用法
1.1大部分函数用法
1.2关于向上取整和向下取整
三、其他函数
1、显示当前正在使用的数据库
2、查询当前用户
3、md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串(哈希加密)
4、password()函数,MySQL数据库使用该函数对用户加密
5、ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值
current_date() |
当前日期 |
current_time() |
当前时间 |
current_timestamp() |
当前时间戳 |
now() |
当前日期时间 |
date(datetime) |
返回datetime参数的日期部分 |
date_add(date,interval d_value_type) |
让date的日期或时间增加。 interval后的数值单位可以是:year minute second day |
date_sub(date,interval d_value_type) |
让date的日期或时间减少。 interval后的数值单位可以是:year minute second day |
datediff(date1,date2) |
两个日期的差,单位是天 |
--current_date()获取日期
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2023-06-19 |
+----------------+
1 row in set (0.00 sec)
--current_time()获取时间
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 08:41:52 |
+----------------+
1 row in set (0.02 sec)
--current_timestamp()获取时间戳
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2023-06-19 08:42:51 |
+---------------------+
1 row in set (0.00 sec)
--now()获取当前日期时间(结果和时间戳函数一样)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2023-06-19 08:44:32 |
+---------------------+
1 row in set (0.00 sec)
--date(datetime)返回datetime参数的日期部分
mysql> select date('2000-8-05');
+-------------------+
| date('2000-8-05') |
+-------------------+
| 2000-08-05 |
+-------------------+
1 row in set (0.00 sec)
mysql> select date('2000-8-05 10:18:21');
+----------------------------+
| date('2000-8-05 10:18:21') |
+----------------------------+
| 2000-08-05 |
+----------------------------+
1 row in set (0.00 sec)
mysql> select date(current_timestamp());
+---------------------------+
| date(current_timestamp()) |
+---------------------------+
| 2023-06-19 |
+---------------------------+
1 row in set (0.00 sec)
mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2023-06-19 |
+-------------+
1 row in set (0.00 sec)
--date_add(date,interval d_value_type)增加时间
mysql> select date_add('2020-10-1',interval 10 day);
+---------------------------------------+
| date_add('2020-10-1',interval 10 day) |
+---------------------------------------+
| 2020-10-11 |
+---------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add(now(),interval 10 day);
+---------------------------------+
| date_add(now(),interval 10 day) |
+---------------------------------+
| 2023-06-29 08:55:39 |
+---------------------------------+
1 row in set (0.00 sec)
--date_sub(date,interval d_value_type)减少时间
mysql> select date_sub(now(),interval 10 day);
+---------------------------------+
| date_sub(now(),interval 10 day) |
+---------------------------------+
| 2023-06-09 08:57:21 |
+---------------------------------+
1 row in set (0.00 sec)
--datediff(date1,date2)两个日期的时间差,单位是天
mysql> select datediff(now(),'2029-10-1');
+-----------------------------+
| datediff(now(),'2029-10-1') |
+-----------------------------+
| -2296 |
+-----------------------------+
1 row in set (0.00 sec)
--创建表
mysql> create table tmp(
-> id bigint primary key auto_increment,
-> birthday date not null
-> );
Query OK, 0 rows affected (0.24 sec)
--插入数据
mysql> insert into tmp (birthday) values ('2000-08-05');
Query OK, 1 row affected (0.02 sec)
mysql> insert into tmp (birthday) values ('1800-10-09');
Query OK, 1 row affected (0.04 sec)
--也可以通过函数插入,插入时间,mysql也会得到日期
mysql> insert into tmp (birthday) values (current_time());
Query OK, 1 row affected, 1 warning (0.03 sec)
--也可以插入时间戳,表会拿走日期部分
mysql> insert into tmp (birthday) values (current_timestamp());
Query OK, 1 row affected, 1 warning (0.03 sec)
--推荐这种写法,方便别人知道插入的就是日期
mysql> insert into tmp (birthday) values (date(current_timestamp()));
Query OK, 1 row affected (0.05 sec)
--打印表
mysql> select* from tmp;
+----+------------+
| id | birthday |
+----+------------+
| 1 | 2000-08-05 |
| 2 | 1800-10-09 |
| 3 | 2023-06-19 |
| 4 | 2023-06-19 |
| 5 | 2023-06-19 |
+----+------------+
5 rows in set (0.01 sec)
--创建表
mysql> create table msg(
-> id bigint primary key auto_increment,
-> content varchar(100) not null,
-> sendtime datetime
-> );
Query OK, 0 rows affected (0.26 sec)
--插入数据
mysql> insert into msg (content,sendtime) values ('评论1',now());
Query OK, 1 row affected (0.03 sec)
--打印时间戳
mysql> select* from msg;
+----+---------+---------------------+
| id | content | sendtime |
+----+---------+---------------------+
| 1 | 评论1 | 2023-06-19 09:39:18 |
+----+---------+---------------------+
1 row in set (0.00 sec)
--打印时间部分
mysql> select content,date(sendtime) from msg;
+---------+----------------+
| content | date(sendtime) |
+---------+----------------+
| 评论1 | 2023-06-19 |
+---------+----------------+
1 row in set (0.00 sec)
--插入一条新评论
mysql> insert into msg (content,sendtime) values ('评论2',now());
Query OK, 1 row affected (0.03 sec)
--查看表
mysql> select * from msg;
+----+---------+---------------------+
| id | content | sendtime |
+----+---------+---------------------+
| 1 | 评论1 | 2023-06-19 09:39:18 |
| 2 | 评论2 | 2023-06-19 09:56:23 |
+----+---------+---------------------+
2 rows in set (0.02 sec)
--进行筛选
mysql> select content,sendtime from msg where sendtime > date_sub(now(),interval 3 minute);
+---------+---------------------+
| content | sendtime |
+---------+---------------------+
| 评论2 | 2023-06-19 09:56:23 |
+---------+---------------------+
1 row in set (0.00 sec)
charset(str) |
返回字符串字符集 |
concat(string2 [, ... ]) |
连接字符串 |
instr(string,substring) |
返回substring在string中的位置,不存在返回0 |
ucase(string2) |
转换成大写 |
lcase(string2) |
转换成小写 |
left(string2,length) |
从string2中,向左截取length个字符 |
right(string2,length) |
从string2中,向右截取length个字符 |
length(string) |
string的长度 |
replace(str,search_str,replace_str) |
将str中的search_str替换为replace_str |
strcmp(string1,string2) |
逐字符比较字符串大小 |
substring(str,position [,length]) |
从str的postion开始,取length个字符 |
ltrim(string) rtrim(string) trim(string) |
去除左空格或右空格或左右空格 |
--charset(str)返回字符串字符集
mysql> select charset('abc');
+----------------+
| charset('abc') |
+----------------+
| utf8 |
+----------------+
1 row in set (0.00 sec)
--concat(string2 [, ... ])字符串连接
mysql> select concat('a','b',3.14);
+----------------------+
| concat('a','b',3.14) |
+----------------------+
| ab3.14 |
+----------------------+
1 row in set (0.04 sec)
--instr(string,substring)返回substring在string中的位置,不存在返回0
mysql> select instr('abc123','23');
+----------------------+
| instr('abc123','23') |
+----------------------+
| 5 |
+----------------------+
1 row in set (0.00 sec)
--ucase(string2)转换成大写
mysql> select ucase('aBc123');
+-----------------+
| ucase('aBc123') |
+-----------------+
| ABC123 |
+-----------------+
1 row in set (0.00 sec)
--ucase(string2)转换成小写
mysql> select lcase('aBc123');
+-----------------+
| lcase('aBc123') |
+-----------------+
| abc123 |
+-----------------+
1 row in set (0.00 sec)
--left(string2,length)从string2中,向左截取length个字符
mysql> select left('aabcd',2);
+-----------------+
| left('aabcd',2) |
+-----------------+
| aa |
+-----------------+
1 row in set (0.01 sec)
--right(string2,length)从string2中,向右截取length个字符
mysql> select right('aabcd',2);
+------------------+
| right('aabcd',2) |
+------------------+
| cd |
+------------------+
1 row in set (0.00 sec)
--string的长度
mysql> select length('aabcd');
+-----------------+
| length('aabcd') |
+-----------------+
| 5 |
+-----------------+
1 row in set (0.00 sec)
--如果表乱码了,可以打印它的字符集看一下
mysql> select charset(content) from msg;
+------------------+
| charset(content) |
+------------------+
| utf8 |
| utf8 |
+------------------+
2 rows in set (0.00 sec)
发现表乱码可以打印出它的字符集,看一下是否是字符编码问题。
mysql> select concat('姓名:',name,'总分:',chinese+math+english,'语文:',chinese,'数学:',math,'英语:',english) from exam_result;
+--------------------------------------------------------------------------------------------------------------------+
| concat('姓名:',name,'总分:',chinese+math+english,'语文:',chinese,'数学:',math,'英语:',english) |
+--------------------------------------------------------------------------------------------------------------------+
| 姓名:唐三藏总分:288语文:134数学:98英语:56 |
| 姓名:曹孟德总分:267语文:140数学:60英语:67 |
| 姓名:刘玄德总分:270语文:110数学:115英语:45 |
| 姓名:孙权总分:291语文:140数学:73英语:78 |
| 姓名:宋公明总分:275语文:150数学:95英语:30 |
| NULL |
+--------------------------------------------------------------------------------------------------------------------+
6 rows in set (0.01 sec)
mysql> select name,length(name) from exam_result;
+-----------+--------------+
| name | length(name) |
+-----------+--------------+
| 唐三藏 | 6 |
| 曹孟德 | 6 |
| 刘玄德 | 6 |
| 孙权 | 4 |
| 宋公明 | 6 |
| 张三 | 4 |
+-----------+--------------+
6 rows in set (0.00 sec)
mysql> select charset(name) from exam_result;
+---------------+
| charset(name) |
+---------------+
| gbk |
| gbk |
| gbk |
| gbk |
| gbk |
| gbk |
+---------------+
6 rows in set (0.00 sec)
注意:length函数返回字符串长度,以字节为单位。如果是多字节字符则计算多个字节数;如果是单字节字符则算作一个字节。比如:字母,数字算作一个字节,中文表示多个字节数(与字符集编码有关)
mysql> select name,replace(name,'张','王五') from exam_result;
+-----------+------------------------------+
| name | replace(name,'张','王五') |
+-----------+------------------------------+
| 唐三藏 | 唐三藏 |
| 曹孟德 | 曹孟德 |
| 刘玄德 | 刘玄德 |
| 孙权 | 孙权 |
| 宋公明 | 宋公明 |
| 张三 | 王五三 |
+-----------+------------------------------+
6 rows in set (0.00 sec)
mysql> select* from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 134 | 98 | 56 |
| 4 | 曹孟德 | 140 | 60 | 67 |
| 5 | 刘玄德 | 110 | 115 | 45 |
| 6 | 孙权 | 140 | 73 | 78 |
| 7 | 宋公明 | 150 | 95 | 30 |
| 8 | 张三 | NULL | NULL | NULL |
+----+-----------+---------+------+---------+
6 rows in set (0.00 sec)
注意select配合replace并不会修改原始数据库。
mysql> select name,substring(name,2,2) from exam_result;
+-----------+---------------------+
| name | substring(name,2,2) |
+-----------+---------------------+
| 唐三藏 | 三藏 |
| 曹孟德 | 孟德 |
| 刘玄德 | 玄德 |
| 孙权 | 权 |
| 宋公明 | 公明 |
| 张三 | 三 |
+-----------+---------------------+
6 rows in set (0.00 sec)
select concat(lcase(substring(ename, 1, 1)),substring(ename,2)) from EMP;
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)
abs(number) |
绝对值函数 |
bin(decimal_number) |
十进制转二进制 |
hex(decimalNumber) |
转换成十六进制 |
conv(number,from_base,to_base) |
进制转换 |
ceiling(number) |
向上取整 |
floor(number) |
向下取整 |
format(number,decimal_places) |
格式化,保留小数位数 |
hex(decimalNumber) |
转换成十六进制 |
rand() |
返回随机浮点数,范围[0.0,1.0) |
mod(number,denominator) |
取模 |
--获取绝对值
mysql> select abs(-65);
+----------+
| abs(-65) |
+----------+
| 65 |
+----------+
1 row in set (0.00 sec)
--十进制转二进制
mysql> select bin(10.863);
+-------------+
| bin(10.863) |
+-------------+
| 1010 |
+-------------+
1 row in set (0.00 sec)
--转换为十六进制
mysql> select hex(12.85);
+------------+
| hex(12.85) |
+------------+
| D |
+------------+
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(3.1415926,2);
+---------------------+
| format(3.1415926,2) |
+---------------------+
| 3.14 |
+---------------------+
1 row in set (0.00 sec)
--取模
mysql> select mod(10,3);
+-----------+
| mod(10,3) |
+-----------+
| 1 |
+-----------+
1 row in set (0.00 sec)
--生成0.0<=x<1.0的小数/生成随机数
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.8152278352670631 |
+--------------------+
1 row in set (0.00 sec)
mysql> select format(rand()*100,0);
+----------------------+
| format(rand()*100,0) |
+----------------------+
| 76 |
+----------------------+
1 row in set (0.00 sec)
向上取整:
mysql> select ceiling(3.14);
+---------------+
| ceiling(3.14) |
+---------------+
| 4 |
+---------------+
1 row in set (0.01 sec)
mysql> select ceiling(-3.14);
+----------------+
| ceiling(-3.14) |
+----------------+
| -3 |
+----------------+
1 row in set (0.00 sec)
向上取整得到的数字比之前的大。
向下取整:
mysql> select floor(3.14);
+-------------+
| floor(3.14) |
+-------------+
| 3 |
+-------------+
1 row in set (0.01 sec)
mysql> select floor(-3.14);
+--------------+
| floor(-3.14) |
+--------------+
| -4 |
+--------------+
1 row in set (0.01 sec)
向下取整得到的数字比之前的小。
mysql> select database();
+------------+
| database() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)
mysql> select user();
+--------+
| user() |
+--------+
| jly@ |
+--------+
1 row in set (0.02 sec)
--创建表
mysql> create table user(
-> id bigint primary key auto_increment,
-> name varchar(20) not null,
-> password char(32) not null
-> );
Query OK, 0 rows affected (0.24 sec)
--插入密码
mysql> insert into user (name,password) values ('张三','abc123');
Query OK, 1 row affected (0.05 sec)
--打印信息,发现明文密码
mysql> select* from user;
+----+--------+----------+
| id | name | password |
+----+--------+----------+
| 1 | 张三 | abc123 |
+----+--------+----------+
1 row in set (0.00 sec)
在数据库中,像密码是绝对不允许明文保存的。需要加密:
--对密码进行md5加密
mysql> insert into user (name,password) values ('李四',md5('abc123'));
Query OK, 1 row affected (0.06 sec)
mysql> select* from user;
+----+--------+----------------------------------+
| id | name | password |
+----+--------+----------------------------------+
| 1 | 张三 | abc123 |
| 2 | 李四 | e99a18c428cb38d5f260853678922e03 |
+----+--------+----------------------------------+
2 rows in set (0.01 sec)
--根据姓名和哈希密码筛选对应的数据
mysql> select name,password from user where name='李四' and password=md5('abc123');
+--------+----------------------------------+
| name | password |
+--------+----------------------------------+
| 李四 | e99a18c428cb38d5f260853678922e03 |
+--------+----------------------------------+
1 row in set (0.00 sec)
mysql> select password('123456');
+-------------------------------------------+
| password('123456') |
+-------------------------------------------+
| *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-------------------------------------------+
1 row in set, 1 warning (0.02 sec)
也可以使用password函数对密码进行加密。
mysql> select ifnull('abc',12);
+------------------+
| ifnull('abc',12) |
+------------------+
| abc |
+------------------+
1 row in set (0.01 sec)
mysql> select ifnull(null,12);
+-----------------+
| ifnull(null,12) |
+-----------------+
| 12 |
+-----------------+
1 row in set (0.00 sec)