mysql 常用函数

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: 字符串函数mysql> select concat('a','b','c'),concat('a',null);+---------------------+------------------+| concat('a','b','c') | c...

字符串函数

这里写图片描述
这里写图片描述

mysql> select concat('a','b','c'),concat('a',null);
+---------------------+------------------+
| concat('a','b','c') | concat('a',null) |
+---------------------+------------------+
| abc                 | NULL             |
+---------------------+------------------+
1 row in set (0.00 sec)
mysql> select insert('beijing2008you',12,3,'me');
+------------------------------------+
| insert('beijing2008you',12,3,'me') |
+------------------------------------+
| beijing2008me                      |
+------------------------------------+
1 row in set (0.01 sec)
mysql> select lower('BEIJING'),upper('beijing');
+------------------+------------------+
| lower('BEIJING') | upper('beijing') |
+------------------+------------------+
| beijing          | BEIJING          |
+------------------+------------------+
1 row in set (0.00 sec)
mysql> select left('beijing2008',7),left('beijing2008',null),right('beijing2008',4);
+-----------------------+--------------------------+------------------------+
| left('beijing2008',7) | left('beijing2008',null) | right('beijing2008',4) |
+-----------------------+--------------------------+------------------------+
| beijing               | NULL                     | 2008                   |
+-----------------------+--------------------------+------------------------+
1 row in set (0.00 sec)
mysql> select lpad('2008',20,'beijing'),rpad('beijing',20,'2008');
+---------------------------+---------------------------+
| lpad('2008',20,'beijing') | rpad('beijing',20,'2008') |
+---------------------------+---------------------------+
| beijingbeijingbe2008      | beijing2008200820082      |
+---------------------------+---------------------------+
1 row in set (0.00 sec)
mysql> select ltrim(' |beijing'),rtrim('beijing|  ');
+--------------------+---------------------+
| ltrim(' |beijing') | rtrim('beijing|  ') |
+--------------------+---------------------+
| |beijing           | beijing|            |
+--------------------+---------------------+
1 row in set (0.00 sec)
mysql> select repeat('mysql ',3);
+--------------------+
| repeat('mysql ',3) |
+--------------------+
| mysql mysql mysql  |
+--------------------+
1 row in set (0.00 sec)
mysql> select replace('beijing_2010','_2010','2008');
+----------------------------------------+
| replace('beijing_2010','_2010','2008') |
+----------------------------------------+
| beijing2008                            |
+----------------------------------------+
1 row in set (0.00 sec)
mysql> select strcmp('a','b'),strcmp('b','b'),strcmp('c','b');
+-----------------+-----------------+-----------------+
| strcmp('a','b') | strcmp('b','b') | strcmp('c','b') |
+-----------------+-----------------+-----------------+
|              -1 |               0 |               1 |
+-----------------+-----------------+-----------------+
1 row in set (0.00 sec)
mysql> select trim(' $ beijing $ ');
+-----------------------+
| trim(' $ beijing $ ') |
+-----------------------+
| $ beijing $           |
+-----------------------+
1 row in set (0.00 sec)
mysql> select substring('beijing2008',8,4),substring('beijing2008',1,7);
+------------------------------+------------------------------+
| substring('beijing2008',8,4) | substring('beijing2008',1,7) |
+------------------------------+------------------------------+
| 2008                         | beijing                      |
+------------------------------+------------------------------+
1 row in set (0.00 sec)

数值函数

这里写图片描述

mysql> select abs(-0.8),abs(0.8);
+-----------+----------+
| abs(-0.8) | abs(0.8) |
+-----------+----------+
|       0.8 |      0.8 |
+-----------+----------+
1 row in set (0.01 sec)

mysql> select ceil(-0.8),ceil(0.8);
+------------+-----------+
| ceil(-0.8) | ceil(0.8) |
+------------+-----------+
|          0 |         1 |
+------------+-----------+
1 row in set (0.02 sec)

mysql> select floor(-0.8),floor(0.8);
+-------------+------------+
| floor(-0.8) | floor(0.8) |
+-------------+------------+
|          -1 |          0 |
+-------------+------------+
1 row in set (0.00 sec)

mysql> select mod(15,10),mod(1,11),mod(null,10);
+------------+-----------+--------------+
| mod(15,10) | mod(1,11) | mod(null,10) |
+------------+-----------+--------------+
|          5 |         1 |         NULL |
+------------+-----------+--------------+
1 row in set (0.01 sec)

mysql> select rand(),rand();
+---------------------+---------------------+
| rand()              | rand()              |
+---------------------+---------------------+
| 0.14735915059909147 | 0.09535266654133114 |
+---------------------+---------------------+
1 row in set (0.00 sec)

mysql> select ceil(100*rand()),ceil(100*rand());
+------------------+------------------+
| ceil(100*rand()) | ceil(100*rand()) |
+------------------+------------------+
|                4 |               89 |
+------------------+------------------+
1 row in set (0.01 sec)

mysql> select round(1.1),round(1.1,2),round(1,2);
+------------+--------------+------------+
| round(1.1) | round(1.1,2) | round(1,2) |
+------------+--------------+------------+
|          1 |         1.10 |          1 |
+------------+--------------+------------+
1 row in set (0.01 sec)

mysql> select round(1.235,2),truncate(1.235,2);
+----------------+-------------------+
| round(1.235,2) | truncate(1.235,2) |
+----------------+-------------------+
|           1.24 |              1.23 |
+----------------+-------------------+
1 row in set (0.00 sec)

日期和时间函数

这里写图片描述

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2016-09-21 |
+------------+
1 row in set (0.00 sec)

mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 10:22:39  |
+-----------+
1 row in set (0.00 sec)

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2016-09-21 10:22:48 |
+---------------------+
1 row in set (0.00 sec)

mysql> select unix_timestamp(now());
+-----------------------+
| unix_timestamp(now()) |
+-----------------------+
|            1474424612 |
+-----------------------+
1 row in set (0.00 sec)

mysql> select from_unixtime(1474424612);
+---------------------------+
| from_unixtime(1474424612) |
+---------------------------+
| 2016-09-21 10:23:32       |
+---------------------------+
1 row in set (0.00 sec)

mysql> select from_unixtime(1);
+---------------------+
| from_unixtime(1)    |
+---------------------+
| 1970-01-01 08:00:01 |
+---------------------+
1 row in set (0.00 sec)

mysql> select week(now()),year(now());
+-------------+-------------+
| week(now()) | year(now()) |
+-------------+-------------+
|          38 |        2016 |
+-------------+-------------+
1 row in set (0.00 sec)

mysql> select hour(now()),minute(now());
+-------------+---------------+
| hour(now()) | minute(now()) |
+-------------+---------------+
|          10 |            27 |
+-------------+---------------+
1 row in set (0.00 sec)

mysql> select monthname(now());
+------------------+
| monthname(now()) |
+------------------+
| September        |
+------------------+
1 row in set (0.02 sec)

mysql日期和时间格式
这里写图片描述
这里写图片描述

mysql> select date_format(now(),'%M,%D,%Y');
+-------------------------------+
| date_format(now(),'%M,%D,%Y') |
+-------------------------------+
| September,21st,2016           |
+-------------------------------+
1 row in set (0.01 sec)

mysql日期间隔类型
这里写图片描述

mysql> select now() current, date_add(now(),interval 31 day) after31days;
+---------------------+---------------------+
| current             | after31days         |
+---------------------+---------------------+
| 2016-09-21 11:08:33 | 2016-10-22 11:08:33 |
+---------------------+---------------------+
1 row in set (0.00 sec)

mysql> select now() current, date_add(now(),interval -31 day) before31days,date_add(now(),interval '-1_-2' year_month) before_oneyear_twomonth;
+---------------------+---------------------+-------------------------+
| current             | before31days        | before_oneyear_twomonth |
+---------------------+---------------------+-------------------------+
| 2016-09-21 11:10:34 | 2016-08-21 11:10:34 | 2015-07-21 11:10:34     |
+---------------------+---------------------+-------------------------+
1 row in set (0.00 sec)
mysql> select datediff('2015-07-06',now());
+------------------------------+
| datediff('2015-07-06',now()) |
+------------------------------+
|                         -443 |
+------------------------------+
1 row in set (0.00 sec)

流程函数

这里写图片描述

测试数据

mysql> create table salary(userid int,salary decimal(9,2));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into salary values (1,1000),(2,2000),(3,3000),(4,4000),(5,5000),(1,null);
Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select * from salary;
+--------+---------+
| userid | salary  |
+--------+---------+
|      1 | 1000.00 |
|      2 | 2000.00 |
|      3 | 3000.00 |
|      4 | 4000.00 |
|      5 | 5000.00 |
|      1 |    NULL |
+--------+---------+
6 rows in set (0.00 sec)
mysql> select userid, if(salary>2000,'high','low') from salary;
+--------+------------------------------+
| userid | if(salary>2000,'high','low') |
+--------+------------------------------+
|      1 | low                          |
|      2 | low                          |
|      3 | high                         |
|      4 | high                         |
|      5 | high                         |
|      1 | low                          |
+--------+------------------------------+
6 rows in set (0.00 sec)

mysql> select userid,ifnull(salary,0) from salary;
+--------+------------------+
| userid | ifnull(salary,0) |
+--------+------------------+
|      1 |          1000.00 |
|      2 |          2000.00 |
|      3 |          3000.00 |
|      4 |          4000.00 |
|      5 |          5000.00 |
|      1 |             0.00 |
+--------+------------------+
6 rows in set (0.00 sec)
mysql> select userid, case when salary<=2000 then 'low' else 'high' end from salary;
+--------+---------------------------------------------------+
| userid | case when salary<=2000 then 'low' else 'high' end |
+--------+---------------------------------------------------+
|      1 | low                                               |
|      2 | low                                               |
|      3 | high                                              |
|      4 | high                                              |
|      5 | high                                              |
|      1 | high                                              |
+--------+---------------------------------------------------+
6 rows in set (0.00 sec)

mysql> select userid,case salary when 1000 then 'low' when 2000 then 'mid' else 'high' end from salary;
+--------+-----------------------------------------------------------------------+
| userid | case salary when 1000 then 'low' when 2000 then 'mid' else 'high' end |
+--------+-----------------------------------------------------------------------+
|      1 | low                                                                   |
|      2 | mid                                                                   |
|      3 | high                                                                  |
|      4 | high                                                                  |
|      5 | high                                                                  |
|      1 | high                                                                  |
+--------+-----------------------------------------------------------------------+
6 rows in set (0.00 sec)

其他函数

这里写图片描述

mysql> select database();
+------------+
| database() |
+------------+
| test       |
+------------+
1 row in set (0.00 sec)

mysql> select version();
+------------+
| version()  |
+------------+
| 5.6.31-log |
+------------+
1 row in set (0.00 sec)

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> select inet_aton('192.168.0.7');
+--------------------------+
| inet_aton('192.168.0.7') |
+--------------------------+
|               3232235527 |
+--------------------------+
1 row in set (0.00 sec)

mysql> select inet_ntoa(3232235527);
+-----------------------+
| inet_ntoa(3232235527) |
+-----------------------+
| 192.168.0.7           |
+-----------------------+
1 row in set (0.00 sec)

使用inet_aton查看192.168.1.3和192.168.1.20之间有多少个ip地址

mysql> create table ipaddr (ip varchar(15));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into ipaddr values ('192.168.1.1'),('192.168.1.3'),('192.168.1.6'),('192.168.1.10'),('192.168.1.20'),('192.168.1.30');
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select * from ipaddr;
+--------------+
| ip           |
+--------------+
| 192.168.1.1  |
| 192.168.1.3  |
| 192.168.1.6  |
| 192.168.1.10 |
| 192.168.1.20 |
| 192.168.1.30 |
+--------------+
6 rows in set (0.00 sec)


mysql> select * from ipaddr where inet_aton(ip)>=inet_aton('192.168.1.3') and inet_aton(ip)<=inet_aton('192.168.1.20');
+--------------+
| ip           |
+--------------+
| 192.168.1.3  |
| 192.168.1.6  |
| 192.168.1.10 |
| 192.168.1.20 |
+--------------+
4 rows in set (0.00 sec)

password用户系统用户,md5用于应用

mysql> select password('123456');
+-------------------------------------------+
| password('123456')                        |
+-------------------------------------------+
| *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-------------------------------------------+
1 row in set (0.00 sec)

mysql> select md5('123456');
+----------------------------------+
| md5('123456')                    |
+----------------------------------+
| e10adc3949ba59abbe56e057f20f883e |
+----------------------------------+
1 row in set (0.00 sec)
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
2月前
|
JSON 关系型数据库 MySQL
Mysql(5)—函数
MySQL提供了许多内置的函数以帮助用户进行数据操作和分析。这些函数可以分为几类,包括聚合函数、字符串函数、数值函数、日期和时间函数、控制流函数等。
77 1
Mysql(5)—函数
|
25天前
|
关系型数据库 MySQL Serverless
MySQL函数
最常用的MySQL函数,包括聚合函数,字符串函数,日期时间函数,控制流函数等
|
2月前
|
存储 SQL 关系型数据库
MySQL 存储函数及调用
MySQL 存储函数及调用
106 3
|
29天前
|
SQL NoSQL 关系型数据库
|
2月前
|
缓存 关系型数据库 MySQL
MySQL 满足条件函数中使用查询最大值函数
MySQL 满足条件函数中使用查询最大值函数
134 1
|
3月前
|
存储 SQL 关系型数据库
MySQL基础:函数
本文介绍了MySQL中几种常用的内建函数,包括字符串函数、数值函数、日期函数和流程函数。字符串函数如`CONCAT()`用于拼接字符串,`TRIM()`用于去除字符串两端的空格,`MOD()`求余数,`RAND()`生成随机数,`ROUND()`四舍五入。日期函数如`CURDATE()`返回当前日期,`NOW()`返回当前日期和时间,`DATE_ADD()`添加时间间隔,`DATEDIFF()`计算日期差。流程函数如`IF()`和`CASE WHEN THEN ELSE END`用于条件判断。聚合函数如`COUNT()`统计行数,`SUM()`求和,`AVG()`求平均值
30 8
MySQL基础:函数
|
2月前
|
关系型数据库 MySQL 数据库
mysql中tonumber函数使用要注意什么
在处理这类转换操作时,考虑周全,利用提供的高性能云服务器资源,可以进一步提升数据库处理效率,确保数据操作的稳定性和安全性,尤其是在处理大量数据转换和运算密集型应用时。
102 0
|
2月前
|
关系型数据库 MySQL 数据处理
企业级应用 mysql 日期函数变量,干货已整理
本文详细介绍了如何在MySQL8.0中使用DATE_FORMAT函数进行日期格式的转换,包括当日、昨日及不同时间段的数据获取,并提供了实际的ETL应用场景和注意事项,有助于提升数据处理的灵活性和一致性。
43 0
|
3月前
|
JSON 关系型数据库 MySQL
MySQL 8.0常用函数汇总与应用实例
这些函数只是MySQL 8.0提供的众多强大功能的一部分。通过结合使用这些函数,你可以有效地处理各种数据,优化数据库查询,并提高应用程序的性能和效率。
67 3
|
4月前
|
SQL 关系型数据库 MySQL
在 MySQL 中使用 `RIGHT` 函数
【8月更文挑战第8天】
386 7
在 MySQL 中使用 `RIGHT` 函数