【MySQL--07】内置函数

【MySQL–07】内置函数

文章目录

  • 【MySQL--07】内置函数
    • 1.函数
      • 1.1日期函数
        • 1.1.1案例1
        • 1.1.2案例2
      • 1.2字符串函数
      • 1.3数字函数
      • 1.4其他函数

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)

(本篇完)

你可能感兴趣的:(MySQL,mysql,数据库,sql)