Custom function:
User-defined function (UDF) is a way to extend MySQL. Its usage is the same as the built-in The functions are the same.
There are two necessary conditions for custom functions:
1. Parameters
2. Return value
The function can return any type of value and can also receive these types. parameters.
There is no necessary internal connection between function parameters and return value.
Example 1:
Create a parameterless function that returns the current time, year, month, day, hours, minutes and seconds.
For example:
mysql> SELECT NOW(); +---------------------+ | NOW() | +---------------------+ | 2016-09-08 21:17:17 | +---------------------+ 1 row in set (0.00 sec) mysql> SELECT DATE_FORMAT(NOW(),'%Y年%m月%d日 %H点:%i分:%s秒'); +--------------------------------------------------+ | DATE_FORMAT(NOW(),'%Y年%m月%d日 %H点:%i分:%s秒') | +--------------------------------------------------+ | 2016年09月08日 21点:19分:54秒 | +--------------------------------------------------+ 1 row in set (0.01 sec)
Encapsulate the above process:
mysql> CREATE FUNCTION f1() RETURNS VARCHAR(30) -> RETURN DATE_FORMAT(NOW(),'%Y年%m月%d日 %H点:%i分:%s秒');Query OK, 0 rows affected (0.08 sec) mysql> SELECT f1(); +-------------------------------+ | f1() | +-------------------------------+ | 2016年09月08日 21点:21分:25秒 | +-------------------------------+ 1 row in set (0.02 sec)
Note: What follows returns is the return value type, and what follows return is the return value.
Example 2:
Create a function with parameters, such as finding the average of two numbers.
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)
Call:
mysql> SELECT f2(4,6); +---------+ | f2(4,6) | +---------+ | 5.00 | +---------+ 1 row in set (0.02 sec)
If you want to delete the two custom functions created above:
mysql> DROP FUNCTION f2;Query OK, 0 rows affected (0.11 sec) mysql> DROP FUNCTION f1;Query OK, 0 rows affected (0.00 sec)
Create a custom function with a composite structure function body
For example: Create a function to insert data into the table and return the id of the inserted data.
mysql> DESC tdb_test; +------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | first_name | varchar(20) | NO | | NULL | | | last_name | varchar(20) | NO | | NULL | | +------------+------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) mysql> CREATE FUNCTION adduser(first_name VARCHAR(20),last_name VARCHAR(20)) -> RETURNS INT UNSIGNED -> RETURN -> INSERT tdb_test(first_name,last_name) VALUES(first_name,last_name); 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 'tdb_test(first_name,last_name) VALUES(first_name,last_name)' at line 4
At this time we found that after entering the sql statement to be executed, the following semicolon is the current default separator, causing the function to be unable to continue inputting. Therefore, the default delimiter needs to be modified.
mysql> DELIMITER //
means ending with //
mysql> CREATE FUNCTION adduser(first_name VARCHAR(20),last_name VARCHAR(20)) -> RETURNS INT UNSIGNED -> RETURN -> INSERT tdb_test(first_name,last_name) VALUES(first_name,last_name); -> LAST_INSERT_ID(); -> // 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 'tdb_test(first_name,last_name) VALUES(first_name,last_name); LAST_INSERT_ID()' at line 4
also returns an error, because there are two statements to be executed, and begin and end need to be put in to form an aggregate. Let’s look at the correct ones below. practice.
mysql> CREATE FUNCTION adduser(first_name VARCHAR(20),last_name VARCHAR(20)) -> RETURNS INT UNSIGNED -> BEGIN -> INSERT tdb_test(first_name,last_name) VALUES(first_name,last_name); -> RETURN LAST_INSERT_ID(); -> END -> // Query OK, 0 rows affected (0.00 sec)
Change the delimiter back
mysql> DELIMITER ;
The test results are as follows:
mysql> SELECT adduser("ttt","ddd"); +----------------------+ | adduser("ttt","ddd") | +----------------------+ | 5 | +----------------------+ 1 row in set (0.11 sec) mysql> SELECT * FROM tdb_test; +----+------------+-----------+ | id | first_name | last_name | +----+------------+-----------+ | 1 | A | B | | 2 | Jack | Bob | | 3 | tom% | 123 | | 4 | 11 | 22 | | 5 | ttt | ddd | +----+------------+-----------+ 5 rows in set (0.00 sec)
About the function body
The function body is legal The sql statement is composed of;
The function body can be a simple SELECT or INSERT statement;
If the function body is a composite structure, use BEGIN... END statement;
Compound structure can contain declarations, loops, control structures;
Delete function:
DROP FUNCTION [IF EXISTS] function_name
Customized Function:
User-defined function (UDF) is a way to extend MySQL, and its usage is the same as the built-in function.
There are two necessary conditions for custom functions:
1. Parameters
2. Return value
The function can return any type of value and can also receive these types. parameters.
There is no necessary internal connection between function parameters and return value.
Example 1:
Create a parameterless function that returns the current time, year, month, day, hours, minutes and seconds.
For example:
mysql> SELECT NOW(); +---------------------+ | NOW() | +---------------------+ | 2016-09-08 21:17:17 | +---------------------+ 1 row in set (0.00 sec) mysql> SELECT DATE_FORMAT(NOW(),'%Y年%m月%d日 %H点:%i分:%s秒'); +--------------------------------------------------+ | DATE_FORMAT(NOW(),'%Y年%m月%d日 %H点:%i分:%s秒') | +--------------------------------------------------+ | 2016年09月08日 21点:19分:54秒 | +--------------------------------------------------+ 1 row in set (0.01 sec)
Encapsulate the above process:
mysql> CREATE FUNCTION f1() RETURNS VARCHAR(30) -> RETURN DATE_FORMAT(NOW(),'%Y年%m月%d日 %H点:%i分:%s秒');Query OK, 0 rows affected (0.08 sec) mysql> SELECT f1();+-------------------------------+| f1() | +-------------------------------+| 2016年09月08日 21点:21分:25秒 | +-------------------------------+1 row in set (0.02 sec)
Note: What follows returns is the return value type, and what follows return is the return value.
Example 2:
Create a function with parameters, such as finding the average of two numbers.
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)
Call:
mysql> SELECT f2(4,6);+---------+| f2(4,6) | +---------+| 5.00 | +---------+1 row in set (0.02 sec)
If you want to delete the two custom functions created above:
mysql> DROP FUNCTION f2;Query OK, 0 rows affected (0.11 sec) mysql> DROP FUNCTION f1;Query OK, 0 rows affected (0.00 sec)
Create a custom function with a composite structure function body
For example: Create a function to insert data into the table and return the id of the inserted data.
mysql> DESC tdb_test; +------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | first_name | varchar(20) | NO | | NULL | | | last_name | varchar(20) | NO | | NULL | | +------------+------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) mysql> CREATE FUNCTION adduser(first_name VARCHAR(20),last_name VARCHAR(20)) -> RETURNS INT UNSIGNED -> RETURN -> INSERT tdb_test(first_name,last_name) VALUES(first_name,last_name); 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 'tdb_test(first_name,last_name) VALUES(first_name,last_name)' at line 4
At this time we found that after entering the sql statement to be executed, the following semicolon is the current default separator, causing the function to be unable to continue inputting. Therefore, the default delimiter needs to be modified.
mysql> DELIMITER //
means ending with //
mysql> CREATE FUNCTION adduser(first_name VARCHAR(20),last_name VARCHAR(20)) -> RETURNS INT UNSIGNED -> RETURN -> INSERT tdb_test(first_name,last_name) VALUES(first_name,last_name); -> LAST_INSERT_ID(); -> // 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 'tdb_test(first_name,last_name) VALUES(first_name,last_name); LAST_INSERT_ID()' at line 4
also returns an error, because there are two statements to be executed, and begin and end need to be put in to form an aggregate. Let’s look at the correct ones below. practice.
mysql> CREATE FUNCTION adduser(first_name VARCHAR(20),last_name VARCHAR(20)) -> RETURNS INT UNSIGNED -> BEGIN -> INSERT tdb_test(first_name,last_name) VALUES(first_name,last_name); -> RETURN LAST_INSERT_ID(); -> END -> // Query OK, 0 rows affected (0.00 sec)
Change the delimiter back
mysql> DELIMITER ;
The test results are as follows:
mysql> SELECT adduser("ttt","ddd"); +----------------------+ | adduser("ttt","ddd") | +----------------------+ | 5 | +----------------------+ 1 row in set (0.11 sec) mysql> SELECT * FROM tdb_test; +----+------------+-----------+ | id | first_name | last_name | +----+------------+-----------+ | 1 | A | B | | 2 | Jack | Bob | | 3 | tom% | 123 | | 4 | 11 | 22 | | 5 | ttt | ddd | +----+------------+-----------+ 5 rows in set (0.00 sec)
About the function body
The function body is legal The sql statement is composed of;
The function body can be a simple SELECT or INSERT statement;
If the function body is a composite structure, use BEGIN... END statement;
Compound structure can contain declarations, loops, and control structures;
Delete function:
DROP FUNCTION [IF EXISTS] function_name
That’s it The content of MySQL custom functions, please pay attention to the PHP Chinese website (m.sbmmt.com) for more related content!