Read Catalogue
- What is a function
- Differences from stored procedures
- MySQL Self-band function
- Custom functions
What is a function
Functions in MySQL are similar to stored procedures, and are a set of SQL sets;
Differences from stored procedures
function can return value, stored procedure cannot return directly, but have output parameter can output multiple return value;
Functions can be embedded in SQL statements, and stored procedures cannot;
Functions are typically used to implement simpler, targeted functions (such as absolute value, return current time, etc.), and stored procedures are used to implement complex functions such as complex business logic functions.
MySQL Self-band function
MySQL itself has implemented a number of common functions, such as mathematical functions, string functions, date and time functions, and so on, do not enumerate, here are simple to use the next few functions:
To find absolute ABS:
Ask for ASCII:
Time-related (now, current_date, Current_time):
Custom functions
Customize a function to determine if the input parameter is greater than or equal to 10:
--------------------------------function structure for ' func_compare '------------------------------DROP function IF EXISTS ' Func_compare ';D elimiter;; CREATE definer= ' root ' @ ' localhost ' FUNCTION ' func_compare ' (a int) RETURNS varchar ($) CHARSET utf8begin #Routine Body goes here ... IF a >= and RETURN ' is greater than or equal to ten '; ELSE RETURN ' less than ten '; END IF; END;;D Elimiter;
Validation functions:
SELECT Func_compare (9), Func_compare (11)
Execution Result:
Functions in MySQL