函数名称 |
描述 |
CONCAT() |
字符连接 |
CONCAT_WS() |
使用指定的分隔符进行字符连接 |
FORMAT() |
数字格式化 |
LOWER() |
转换成小写字母 |
UPPER() |
转换成大写字母 |
LEFT() |
获取左侧字符 |
RIGHT() |
获取右侧字符 |
LENGTH() |
获取字符串的长度 |
LTRIM |
删除前导空格 |
RTRIM |
删除后续空格 |
TRIM |
删除前导和后续空格 |
SUBSTRING() |
字符串获取 |
[NOT] LIKE |
模式匹配 |
REPLACE() |
字符串替换 |
# 将2个字符连接在一起
mysql> SELECT CONCAT('I love ', 'you!');
+---------------------------+
| CONCAT('I love ', 'you!') |
+---------------------------+
| I love you! |
+---------------------------+
1 row in set (0.00 sec)
# 可指定第二个参数连接符号
mysql> SELECT CONCAT('I love ','--', 'you!');
+--------------------------------+
| CONCAT('I love ','--', 'you!') |
+--------------------------------+
| I love --you! |
+--------------------------------+
1 row in set (0.00 sec)
# 使用指定的分隔符进行字符连接
mysql> SELECT CONCAT_WS('#', 'A', 'B', 'C');
+-------------------------------+
| CONCAT_WS('#', 'A', 'B', 'C') |
+-------------------------------+
| A#B#C |
+-------------------------------+
1 row in set (0.00 sec)
# 数字格式化,第二个参数为保留的小数位数
mysql> SELECT FORMAT(1112333.567, 1);
+------------------------+
| FORMAT(1112333.567, 1) |
+------------------------+
| 1,112,333.6 |
+------------------------+
1 row in set (0.00 sec)
# 将所有字母改为小写
mysql> SELECT LOWER('MySql');
+----------------+
| LOWER('MySql') |
+----------------+
| mysql |
+----------------+
1 row in set (0.00 sec)
# 将所有字符改为大写
mysql> SELECT UPPER('mysql');
+----------------+
| UPPER('mysql') |
+----------------+
| MYSQL |
+----------------+
1 row in set (0.00 sec)
# 取左侧前2个字符
mysql> SELECT LEFT('mysql', 2);
+------------------+
| LEFT('mysql', 2) |
+------------------+
| my |
+------------------+
1 row in set (0.00 sec)
# 取右侧后3个字符
mysql> SELECT RIGHT('mysql', 3);
+-------------------+
| RIGHT('mysql', 3) |
+-------------------+
| sql |
+-------------------+
1 row in set (0.00 sec)
#获取字符串的长度
mysql> SELECT LENGTH('mysql');
+-----------------+
| LENGTH('mysql') |
+-----------------+
| 5 |
+-----------------+
1 row in set (0.00 sec)
# 删除前导字符'$'
mysql> SELECT TRIM(LEADING '$' FROM '$$$mysql$$$$$$$$');
+-------------------------------------------+
| TRIM(LEADING '$' FROM '$$$mysql$$$$$$$$') |
+-------------------------------------------+
| mysql$$$$$$$$ |
+-------------------------------------------+
1 row in set (0.00 sec)
# 删除后续字符'$'
mysql> SELECT TRIM(TRAILING '$' FROM '$$$mysql$$$$$$$$');
+--------------------------------------------+
| TRIM(TRAILING '$' FROM '$$$mysql$$$$$$$$') |
+--------------------------------------------+
| $$$mysql |
+--------------------------------------------+
1 row in set (0.00 sec)
# 删除前导和后续字符
mysql> SELECT TRIM(BOTH '$' FROM '$$$mysql$$$$$$$$');
+----------------------------------------+
| TRIM(BOTH '$' FROM '$$$mysql$$$$$$$$') |
+----------------------------------------+
| mysql |
+----------------------------------------+
1 row in set (0.00 sec)
# 将字符'$'替换为空字符
mysql> SELECT REPLACE('$$$my$$$$sql$$$$$$$$', '$', '');
+------------------------------------------+
| REPLACE('$$$my$$$$sql$$$$$$$$', '$', '') |
+------------------------------------------+
| mysql |
+------------------------------------------+
1 row in set (0.00 sec)
# 截取字符串前2个字符,注意:下标从1开始。第一个参数为字符串,第二个参数为截取字符的起始位置,第三个参数为截取字符的终止位置,都是闭区间!其操作和python切片类似。
mysql> SELECT SUBSTRING('mysql', 1, 2);
+--------------------------+
| SUBSTRING('mysql', 1, 2) |
+--------------------------+
| my |
+--------------------------+
1 row in set (0.00 sec)
# 用LIKE关键字来匹配字符串
mysql> SELECT * FROM cyc;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | 张三 | 20 |
| 2 | 张刘 | 11 |
| 3 | 李四 | 16 |
| 4 | tom% | 16 |
+----+------+-----+
4 rows in set (0.00 sec)
mysql> SELECT * FROM cyc WHERE name LIKE '张%';
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | 张三 | 20 |
| 2 | 张刘 | 11 |
+----+------+-----+
2 rows in set (0.00 sec)
# 匹配含'%'的记录,中间的'%'前面添加字符表示转义,旁边2个为通配符。
mysql> SELECT * FROM cyc WHERE name LIKE '%1%%' ESCAPE '1';
+----+------+-----+
| id | name | age |
+----+------+-----+
| 4 | tom% | 16 |
+----+------+-----+
1 row in set (0.00 sec)
-
%(百分号)
:匹配任意多个字符;
-
-(下划线)
:匹配任意一个字符。
- 数值运算符与函数
名称 |
描述 |
CEIL() |
进一取整 |
DIV |
整数除法(结果没有小数) |
FLOOR() |
舍一取整 |
MOD |
取余数(取模) |
POWER() |
幂运算 |
ROUND() |
四舍五入 |
TRUNCATE() |
数字截取,以小数点为参照点 |
mysql> SELECT TRUNCATE(3.55555,1);
+---------------------+
| TRUNCATE(3.55555,1) |
+---------------------+
| 3.5 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT TRUNCATE(3.55555,0);
+---------------------+
| TRUNCATE(3.55555,0) |
+---------------------+
| 3 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT TRUNCATE(13.55555,-1);
+-----------------------+
| TRUNCATE(13.55555,-1) |
+-----------------------+
| 10 |
+-----------------------+
1 row in set (0.00 sec)
名称 |
描述 |
[NOT] BETWEEN...AND... |
[不]在范围之内(闭合区间) |
[NOT] IN() |
[不]在列出值范围内 |
IS [NOT] NULL |
[不]为空 |
mysql> SELECT 1 BETWEEN 1 AND 23;
+--------------------+
| 1 BETWEEN 1 AND 23 |
+--------------------+
| 1 |
+--------------------+
1 row in set (0.00 sec)
mysql> SELECT 15 IN (2, 3, 4, 54, 100);
+--------------------------+
| 15 IN (2, 3, 4, 54, 100) |
+--------------------------+
| 0 |
+--------------------------+
1 row in set (0.00 sec)
mysql> SELECT NULL IS NULL;
+--------------+
| NULL IS NULL |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM cyc WHERE name IS NOT NULL;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | 张三 | 20 |
| 2 | 张刘 | 11 |
| 3 | 李四 | 16 |
| 4 | tom% | 16 |
+----+------+-----+
4 rows in set (0.00 sec)
名称 |
描述 |
NOW() |
当前日期和时间 |
CURDATE() |
当前日期 |
CURTIME() |
当前时间 |
DATE_ADD() |
日期变化 |
DATEDIFF() |
日期差值 |
DATE_FORMAT() |
日期格式化 |
# 返回当前日期和时间
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2019-08-24 10:27:34 |
+---------------------+
1 row in set (0.00 sec)
# 返回当前日期
mysql> SELECT CURDATE();
+------------+
| CURDATE() |
+------------+
| 2019-08-24 |
+------------+
1 row in set (0.00 sec)
# 返回当前时间
mysql> SELECT CURTIME();
+-----------+
| CURTIME() |
+-----------+
| 10:27:59 |
+-----------+
1 row in set (0.00 sec)
# 使用关键字 INTERVAL 来计算日期,注意:WEEK不为复数形式,不加S
mysql> SELECT DATE_ADD('2019-08-24', INTERVAL 3 WEEK);
+-----------------------------------------+
| DATE_ADD('2019-08-24', INTERVAL 3 WEEK) |
+-----------------------------------------+
| 2019-09-14 |
+-----------------------------------------+
1 row in set (0.00 sec)
# 日期差值计算
mysql> SELECT DATEDIFF('2019-08-24', '2019-10-23');
+--------------------------------------+
| DATEDIFF('2019-08-24', '2019-10-23') |
+--------------------------------------+
| -60 |
+--------------------------------------+
1 row in set (0.00 sec)
# 日期格式化
mysql> SELECT DATE_FORMAT('2018-3-8','%m/%d/%Y');
+------------------------------------+
| DATE_FORMAT('2018-3-8','%m/%d/%Y') |
+------------------------------------+
| 03/08/2018 |
+------------------------------------+
1 row in set (0.00 sec)
名称 |
描述 |
CONNECTION_ID() |
连接ID或者thread线程ID |
DATABASE() |
当前数据库 |
LAST_INSERT_ID() |
最后插入记录的ID号 |
USER() |
当前用户 |
VERSION() |
版本信息 |
# 查询连接ID
mysql> SELECT CONNECTION_ID();
+-----------------+
| CONNECTION_ID() |
+-----------------+
| 3 |
+-----------------+
1 row in set (0.00 sec)
# 当前连接的数据库
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)
mysql> INSERT cyc(name, age) VALUES('nick', 23);
Query OK, 1 row affected (0.00 sec)
# 返回最后插入一条记录的ID,注意:有自增id才有结果,若同时插入多条记录,则返回第一条记录的id值
mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 5 |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM cyc;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | 张三 | 20 |
| 2 | 张刘 | 11 |
| 3 | 李四 | 16 |
| 4 | tom% | 16 |
| 5 | nick | 23 |
+----+------+-----+
5 rows in set (0.00 sec)
# 查看当前用户
mysql> SELECT USER();
+----------------+
| USER() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
# 查看当前版本号
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.26 |
+-----------+
1 row in set (0.00 sec)
名称 |
描述 |
AVG() |
平均值 |
COUNT() |
计数 |
MAX() |
最大值 |
MIN() |
最小值 |
SUM() |
求和 |
名称描述 |
|
MD5() |
信息摘要(加密)算法(主要用在web项目) |
PASSWORD() |
密码计算法(主要用在mysql客户端) |
mysql> SELECT MD5('root');
+----------------------------------+
| MD5('root') |
+----------------------------------+
| 63a9f0ea7bb98050796b649e85481845 |
+----------------------------------+
1 row in set (0.00 sec)
mysql> SELECT PASSWORD('root');
+-------------------------------------------+
| PASSWORD('root') |
+-------------------------------------------+
| *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)
# 提示PASSWORD()这个函数将会被丢弃
mysql> SHOW WARNINGS;
+---------+------+-------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------------------------------+
| Warning | 1681 | 'PASSWORD' is deprecated and will be removed in a future release. |
+---------+------+-------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SET PASSWORD=PASSWORD('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)
# 查看警告
mysql> SHOW WARNINGS;
+---------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1287 | 'SET PASSWORD = PASSWORD('')' is deprecated and will be removed in a future release. Please use SET PASSWORD = '' instead |
+---------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
# 更改客户端登陆密码
mysql> SET PASSWORD='admin';
Query OK, 0 rows affected (0.00 sec)
# 退出客户端
mysql> exit;
Bye
# 重新登陆mysql客户端,回车即可登陆成功
C:\Users\17283>mysql -uroot -padmin
CREATE FUNCTION function_name
RETURNS {STRING | INTEGER | REAL | DECIMAL | VARCHAR(cap)} routine_body;
- 函数体:
1、由合法的sql语句构成;
2、可以是简单的SELECT
或INSERT
语句;
3、若为复合结构
(多条sql语句)则使用BEGIN...END
语句;复合结构可以包含声明,循环,控制结构。
mysql> SELECT DATE_FORMAT(NOW(), '%Y年%m月%d日 %H点:%i分:%s秒');
+-----------------------------------------------------+
| DATE_FORMAT(NOW(), '%Y年%m月%d日 %H点:%i分:%s秒') |
+-----------------------------------------------------+
| 2019年08月24日 11点:16分:41秒 |
+-----------------------------------------------------+
1 row in set (0.00 sec)
# 创建自定义函数,2个必要条件:(零个或多个)参数和(只能有一个)返回值
mysql> CREATE FUNCTION f1() RETURNS VARCHAR(30) RETURN DATE_FORMAT(NOW(), '%Y年%m月%d日 %H点:%i分:%s秒');
Query OK, 0 rows affected (0.00 sec)
# 调用自定义函数
mysql> SELECT f1();
+---------------------------------+
| f1() |
+---------------------------------+
| 2019年08月24日 11点:27分:32秒 |
+---------------------------------+
1 row in set (0.00 sec)
# 创建一个计算平均值的函数,返回结果保留2位小数
mysql> CREATE FUNCTION f2(num1 SMALLINT UNSIGNED,num2 SMALLINT UNSIGNED) RETURNS FLOAT(10, 2) UNSIGNED RETURN (num1 + num2) / 2;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT f2(10.1, 2);
+-------------+
| f2(10.1, 2) |
+-------------+
| 6.00 |
+-------------+
1 row in set (0.00 sec)
# 创建带参数的复合函数,由于mysql默认结束符号是分号';'。
# 因此创建复合函数时会出错,暂时修改默认结束符为'//',创建完之后再修改为分号!
mysql> CREATE FUNCTION adduser(username VARCHAR(20), user_age SMALLINT UNSIGNED) RETURNS INT UNSIGNED
-> BEGIN
-> INSERT cyc(name, age) VALUES(username, user_age);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
mysql> DELIMITER //
mysql> CREATE FUNCTION adduser(username VARCHAR(20), user_age SMALLINT UNSIGNED) RETURNS INT UNSIGNED
-> BEGIN
-> INSERT cyc(name, age) VALUES(username, user_age);
-> RETURN LAST_INSERT_ID();
-> END
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT adduser('王五',56);
-> //
+--------------------+
| adduser('王五',56) |
+--------------------+
| 6 |
+--------------------+
1 row in set (0.00 sec)
# 修改结束符号为分号
mysql> DELIMITER ;
mysql> SELECT adduser('赵六',26);
+--------------------+
| adduser('赵六',26) |
+--------------------+
| 7 |
+--------------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM cyc;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | 张三 | 20 |
| 2 | 张刘 | 11 |
| 3 | 李四 | 16 |
| 4 | tom% | 16 |
| 5 | nick | 23 |
| 6 | 王五 | 56 |
| 7 | 赵六 | 26 |
+----+------+-----+
7 rows in set (0.00 sec)