当前位置:网站首页>MySQL custom function
MySQL custom function
2022-06-13 05:56:00 【A programmer in a wig】
Preface
MySQL It provides built-in functions , The existence of these functions brings great convenience to our daily development and data operation , Like the aggregate function I mentioned earlier SUM()、AVG() And date time functions and so on , But we always have other needs : We need to call a function , This function needs to implement our own functions according to our requirements , However, this functional system cannot provide because of the uncertainty of demand . So we need to solve this demand by ourselves . not so bad MySQL The extensibility of the design gives us this opportunity , We can solve this problem through the function of custom function .
brief introduction
stay MySQL The use of custom functions in also requires corresponding requirements , The grammar is as follows ,
Create a new function :
Create function function_name( parameter list )
returns return type
Function body content
Related instructions
Function name : Should be a legal identifier , And should not conflict with existing keywords . A function should belong to a database , have access to db_name.funciton_name Execute the database to which the current function belongs
, Otherwise, it defaults to the current database .parameter list : You can have one or more function parameters , Even without parameters . For each parameter , It consists of parameter name and parameter type .
Return value : Indicates the return value class type
The body of the function : The function body of a custom function consists of several available MySQL sentence , Process control , Variable declaration and other statements . It should be noted that the function body must contain return Return statement .
Examples of custom functions
[1] Functions without parameters
DELIMITER $$
create function say_hello()
RETURNS VARCHAR(255)
BEGIN
return 'Ha HA HA HA HA Hello!!!!';
END $$
DELIMITER ;
Call function :
select say_hello();
select say_hello(),b.* from book b;
[2] Functions with parameters
DELIMITER $$
create function format_title(title varchar(255))
RETURNS VARCHAR(255)
BEGIN
declare v_title varchar(255);
if length(title)>5 THEN
select CONCAT(SUBSTR(title,1,5),'...') into v_title;
ELSE
set v_title = title;
end if;
return v_title;
END $$
DELIMITER ;
Call functions with parameters :
select format_title(title) from book
边栏推荐
- 12 error end event and terminateendevent of end event
- Sentinel series introduction to service flow restriction
- Leetcode- find all missing numbers in the array - simple
- 2021.9.30学习日志-postman
- OpenGL馬賽克(八)
- [automated test] cypress manual
- Leetcode perfect number simple
- powershell优化之一:提示符美化
- Interrupt processing
- 使用cmake交叉編譯helloworld
猜你喜欢
中断处理过程
3. Postman easy to use
How to view tongweb logs correctly?
Concurrent programming -- source code analysis of thread pool
Concurrent programming -- what is threading?
Experience of redis installation under Linux system (an error is reported at the same time. The struct redis server does not have a member named XXXX)
1 Introduction to drools rule engine (usage scenarios and advantages)
MySQL fuzzy query and sorting by matching degree
[turn] explain awk (1)__ Awk Basics_ Options_ Program segment parsing and examples
powershell优化之一:提示符美化
随机推荐
15 inclusivegateway and eventgateway of flowable gateway
Leetcode- student attendance record i- simple
Ffmpeg download suffix is Video files for m3u8
Summary of the 11th week of sophomore year
Leetcode perfect number simple
You still can't remotely debug idea? Come and have a look at my article. It's easy to use
Etcd fast cluster building
Quartz basic use
Nacos series registry principle and source code analysis
Leetcode- maximum average of subarray i- simple
Service fusing and degradation of Note Series
Software testing - Summary of common interface problems
2021.9.29学习日志-MIME类型
Leetcode- hex number - simple
Leetcode- intersection of two arrays ii- simple
Concurrent programming -- countdownlatch combined with thread pool
Current limiting and fusing of gateway gateway in Spirng cloud
Sentinel series introduction to service flow restriction
Explanation of sentinel series' features, composition and deployment
OpenGL馬賽克(八)