Dieser Artikel bietet Ihnen eine detaillierte Einführung in benutzerdefinierte MySQL-Funktionen und gespeicherte Prozeduren (mit Code). Ich hoffe, dass er für Freunde hilfreich ist.
1. Voraussetzungen
Die Struktur und die Daten sind wie folgt:
mysql> desc user_info; +-----------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+----------+------+-----+---------+-------+ | id | int(10) | NO | PRI | NULL | | | name | char(20) | NO | | NULL | | | passwd | char(40) | NO | | NULL | | | email | char(20) | NO | | NULL | | | phone | char(20) | NO | | NULL | | | role | char(10) | NO | | NULL | | | sex | char(10) | NO | | NULL | | | status | int(10) | NO | | NULL | | | createAt | datetime | NO | | NULL | | | exprAt | datetime | NO | | NULL | | | validDays | int(10) | NO | | NULL | | | delAt | datetime | YES | | NULL | | +-----------+----------+------+-----+---------+-------+ 12 rows in set (0.10 sec) mysql> select * from user_info; +----+--------------+----------+------------+-------------+--------+------+--------+---------------------+---------------------+-----------+-------+ | id | name | passwd | email | phone | role | sex | status | createAt | exprAt | validDays | delAt | +----+--------------+----------+------------+-------------+--------+------+--------+---------------------+---------------------+-----------+-------+ | 1 | StephenWang7 | py123456 | 123@qq.com | 15103887470 | admin | male | 200 | 2019-04-12 20:11:30 | 2019-04-19 20:11:30 | 30 | NULL | | 2 | StephenWang8 | 123456 | 123@qq.com | 15103887470 | viewer | male | 200 | 2019-04-12 20:11:30 | 2019-04-19 20:11:30 | 30 | NULL | +----+--------------+----------+------------+-------------+--------+------+--------+---------------------+---------------------+-----------+-------+ 2 rows in set (0.00 sec)
Die Syntax zum Erstellen einer benutzerdefinierten Funktion (User Defined Function (kurz UDF)) lautet wie folgt:
create function <函数名称> ([参数1] [类型1], [参数N] [类型N]) returns <类型> return <函数主体>
select <函数名称> ([参数])
#定义函数 mysql> create function user_info_count() -> returns int(10) -> return -> (select count(*) from user_info);
mysql> select user_info_count(); +-------------------+ | user_info_count() | +-------------------+ | 2 | +-------------------+ 1 row in set (0.00 sec)
#定义函数 mysql> create function queryNameById(uid int(10)) -> returns char(20) -> return -> (select name from user_info where id=uid); Query OK, 0 rows affected (0.01 sec)
mysql> select queryNameById(1); +------------------+ | queryNameById(1) | +------------------+ | StephenWang7 | +------------------+ 1 row in set (0.00 sec)
show function status;
# show create function 函数名称; mysql> show function queryNameById; 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 'queryNameById' at line 1 mysql> show function queryNameById(); 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 'queryNameById()' at line 1 mysql> show create function queryNameById(); 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 1 mysql> show create function queryNameById; +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ | Function | sql_mode | Create Function | character_set_client | collation_connection | Database Collation | +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ | queryNameById | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=`root`@`localhost` FUNCTION `queryNameById`(uid int(10)) RETURNS char(20) CHARSET latin1 return (select name from user_info where id=uid) | utf8 | utf8_general_ci | latin1_swedish_ci | +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ 1 row in set (0.00 sec
drop function <函数名称>;
mysql> drop function queryNameById; Query OK, 0 rows affected (0.45 sec) mysql> select queryNameById(1); ERROR 1305 (42000): FUNCTION rms.queryNameById does not exist mysql>
Die Syntax zum Definieren einer gespeicherten Prozedur lautet wie folgt:
CREATE PROCEDURE <过程名> ( [过程参数[,…] ] ) <过程体> [过程参数[,…] ] 格式 [ IN | OUT | INOUT ] <参数名> <类型> #语法定义来自:http://c.biancheng.net/view/2593.html
mysql> DELIMITER // mysql> craete procedure queryName() -> begin -> select name from user_info; -> end //
#此时的命令的结束符号为// 不是; mysql> call queryName()// +--------------+ | name | +--------------+ | StephenWang7 | | StephenWang8 | +--------------+ 2 rows in set (0.00 sec) Query OK, 0 rows affected (0.00 sec)
mysql> create procedure queryNameById -> (In uid int(15)) -> begin -> select name from user_info where id=uid; -> end -> // Query OK, 0 rows affected (0.03 sec)
mysql> call queryNameById(1); -> // +--------------+ | name | +--------------+ | StephenWang7 | +--------------+ 1 row in set (0.03 sec) Query OK, 0 rows affected (0.04 sec)
show create procedure <过程名称>
mysql> show create procedure queryNameById; -> // +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ | Procedure | sql_mode | Create Procedure | character_set_client | collation_connection | Database Collation | +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ | queryNameById | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=`root`@`localhost` PROCEDURE `queryNameById`(In uid int(15)) begin select name from user_info where id=uid; end | utf8 | utf8_general_ci | latin1_swedish_ci | +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+ 1 row in set (0.04 sec)
drop procedure <过程名称>
mysql> drop procedure queryNameById// Query OK, 0 rows affected (0.02 sec) mysql> call queryNameById(1)// ERROR 1305 (42000): PROCEDURE rms.queryNameById does not exist
a. Verschiedene Aufrufmethoden
#自定义函数 select <函数名> #存储过程 call <存储过程名>
c. Benutzerdefinierte Funktionen müssen Rückgabeanweisungen enthalten, gespeicherte Prozeduren jedoch nicht.
MySQL-Tutorial]
Das obige ist der detaillierte Inhalt vonDetaillierte Einführung in benutzerdefinierte MySQL-Funktionen und gespeicherte Prozeduren (mit Code). Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!