Functions in MySQL

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.