当前位置:网站首页>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
边栏推荐
- Leetcode minimum absolute difference of binary search tree simple
- Pychart encountered time zone problem when connecting to MySQL timezone
- Concurrent programming -- what is threading?
- Tongweb customs clearance guidelines
- Timeout thread log for tongweb
- 为什么那么多人讨厌A-Spice
- 移动端适配方案
- Tongweb card, tongweb card, tongweb card
- ArrayList loop removes the pit encountered
- Leetcode- reverse string ii- simple
猜你喜欢

Missing tag identification in cots RFID systems: bringing the gap between theory and Practice
![[spark]spark introductory practical series_ 8_ Spark_ Mllib (upper)__ Introduction to machine learning and sparkmllib](/img/39/501b9b8962b03cfc59e2636f88ee93.jpg)
[spark]spark introductory practical series_ 8_ Spark_ Mllib (upper)__ Introduction to machine learning and sparkmllib

arrayList && linkedList

ffmpeg 下载后缀为.m3u8的视频文件

Why do so many people hate a-spice

Vagrant virtual machine installation, disk expansion and LAN access tutorial
![[to]12 common IP commands in the iproute installation package](/img/65/a214d137e230b1a1190feb03660f2c.jpg)
[to]12 common IP commands in the iproute installation package

2 first experience of drools

Etcd understanding of microservice architecture

OpenGL馬賽克(八)
随机推荐
[turn] explain awk (2)_ Combining formatted output with built-in variables to realize requirements
軟件測試——接口常見問題匯總
Config server configuration center of Nacos series
2 first experience of drools
Some methods of string
Mongodb multi field aggregation group by
[to]12 common IP commands in the iproute installation package
Concurrent programming -- countdownlatch combined with thread pool
JNDI configuration for tongweb7
Building a stand-alone version of Nacos series
Quartz database storage
Implementation of concurrent programming locking
Class conflicts caused by tongweb Enterprise Edition and embedded Edition
Vagrant virtual machine installation, disk expansion and LAN access tutorial
Top slide immersive dialog
Leetcode guessing numbers game - simple
Cross compile HelloWorld with cmake
Set the correct width and height of the custom dialog
Four shardingsphere JDBC sharding strategies
Interrupt processing