溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL內(nèi)置函數(shù)和自定義函數(shù)怎么使用

發(fā)布時(shí)間:2022-06-16 10:31:13 來源:億速云 閱讀:212 作者:iii 欄目:開發(fā)技術(shù)

這篇“MySQL內(nèi)置函數(shù)和自定義函數(shù)怎么使用”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“MySQL內(nèi)置函數(shù)和自定義函數(shù)怎么使用”文章吧。

    前言:

    函數(shù)分為兩類:系統(tǒng)函數(shù)和自定義函數(shù)

    使用函數(shù):

    select 函數(shù)名(參數(shù)列表);

    1、內(nèi)置函數(shù)

    1.1、字符串函數(shù)

    函數(shù)名說明
    char_length判斷字符串的字符數(shù)
    length判斷字符串的字節(jié)數(shù),與字符集有關(guān)
    concat連接字符串
    insrt檢查字符是否在目標(biāo)字符串中,存在返回其位置,不存在返回 0
    lcase全部小寫
    ltrim消除左邊的空格
    left(str, length)左側(cè)開始截取字符串,直到指定位置
    right(str, length)右側(cè)開始截取字符串,直到指定位置
    mid從中間指定位置開始截取,如果不指定截取長度,直接到最后
    substring(str, index, [length])從指定位置開始,指定截取長度
    substring_index(str, delim, count)按照關(guān)鍵字截取

    示例:

    select char_length('你好中國'); // 4
    select length('你好中國'); // 12
    select length('hello'); // 5
    select char_length('hello'); // 5
    select concat('你好', '中國'); // 你好中國
    -- 下標(biāo)從 1 開始
    select instr('你好中國', '中國'); // 3
    select instr('你好中國', '我'); // 0
    select lcase('aBcd'); // abcd
    select left('aBcd', 2); // aB
    select right('abcdef', 2); // ef
    select substring('abcdef', 2, 3); // bcd
    select substring('abcdef', -2, 3); // ef
    select ltrim(' abc d '); // abc d
    select mid('你好中國', 3); // 中國
    select substring_index('www.baidu.com', '.', 2); // www.baidu
    select substring_index('www.baidu.com', '.', -2); // baidu.com

    1.2、時(shí)間函數(shù)

    函數(shù)名說明
    now()返回當(dāng)前時(shí)間,日期 時(shí)間
    curdate()當(dāng)前日期
    curtime()當(dāng)前時(shí)間
    datediff()判斷兩個(gè)日期之間的天數(shù)之差,日期使用字符串格式(用引號)
    date_add(日期, interval 時(shí)間數(shù)字 type)時(shí)間增加(type:
    unix_timestamp()獲取時(shí)間戳
    from_unixtime()將指定時(shí)間戳轉(zhuǎn)換成對應(yīng)的日期時(shí)間格式

    示例:

    select now(); // 2022-04-10 22:05:38
    select curdate(); // 2022-04-10
    select curtime(); // 22:05:51
    select datediff('2022-01-09', '2022-01-01'); // 8
    select date_add('2000-10-01', interval 10 day); // 2000-10-11
    select unix_timestamp(); // 1649599799
    select from_unixtime(1649599799); // 2022-04-10 22:09:59

    1.3、數(shù)學(xué)函數(shù)

    函數(shù)名說明
    abs絕對值
    ceiling向上取整
    floor向下取整
    pow指數(shù)
    rand隨機(jī)數(shù)(0-1)
    round四舍五入

    示例:

    select abs(-1); // 1
    select ceiling(1.1); // 2
    select floor(1.9); // 1
    select pow(2, 4); // 16
    select rand(); // 0.2616088308967732
    select round(1.5); // 2

    1.4、其他函數(shù)

    函數(shù)名說明
    md5()MD5
    version()版本號
    database()顯示當(dāng)前所在數(shù)據(jù)庫
    uuid()生成一個(gè)唯一標(biāo)識符,全局唯一

    示例:

    select md5('abc'); // 900150983cd24fb0d6963f7d28e17f72
    select version(); // 8.0.16
    select database(); // mydatabase
    select uuid(); // c44a06a2-b8d8-11ec-a53c-504259f9d746

    2、自定義函數(shù)

    mysql一旦見到分號結(jié)束符,就會開始執(zhí)行

    修改語句結(jié)束符

    基本語法:

    delimiter 符號;

    2.1、創(chuàng)建函數(shù)

    基本語法:

    -- 修改語句結(jié)束符
    delimiter $$;
    create function 函數(shù)名(形參) returns 返回值類型
    begin
        // 函數(shù)體
        return 返回值數(shù)據(jù);
    end
    語句結(jié)束符
    -- 將語句結(jié)束符修改回來
    delimiter ;

    示例:

    -- 修改語句結(jié)束符
    delimiter $$
    create function my_func1() returns int
    begin
        return 10;
    end
    -- 結(jié)束
    $$
    -- 將語句結(jié)束符改回來
    delimiter ;

    如果只有一條語句,可以省略begin 和 end

    -- 最簡單的函數(shù)
    create function foo() returns int
    return 10;

    為函數(shù)的形參指定數(shù)據(jù)類型

    基本語法:

    形參 數(shù)據(jù)類型

    示例:

    create function my_func2(a int, b int) returns int
    return a * b;

    2.2、查看函數(shù)

    基本語法:

    show function status [like 'pattern'];

    示例:

    -- 查看所有函數(shù)
    show function status\G
    -- 查看單個(gè)函數(shù)
    mysql> show function status like 'foo'\G
    *************************** 1. row ***************************
                      Db: mydatabase
                    Name: foo
                    Type: FUNCTION
                 Definer: root@localhost
                Modified: 2022-04-10 22:34:06
                 Created: 2022-04-10 22:34:06
           Security_type: DEFINER
                 Comment:
    character_set_client: utf8mb4
    collation_connection: utf8mb4_general_ci
      Database Collation: utf8mb4_general_ci
    1 row in set (0.00 sec)
    -- 查看函數(shù)創(chuàng)建語句
    mysql> show create function foo\G
    *************************** 1. row ***************************
                Function: foo
                sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
         Create Function: CREATE DEFINER=`root`@`localhost` FUNCTION `foo`() RETURNS int(11)
    return 10
    character_set_client: utf8mb4
    collation_connection: utf8mb4_general_ci
      Database Collation: utf8mb4_general_ci
    1 row in set (0.01 sec)

    2.3、調(diào)用函數(shù)

    基本語法

    select 函數(shù)名(實(shí)參列表);

    示例:

    mysql> select foo();
    +-------+
    | foo() |
    +-------+
    |    10 |
    +-------+
    mysql> select my_func2(2, 3);
    +----------------+
    | my_func2(2, 3) |
    +----------------+
    |              6 |
    +----------------+

    2.4、刪除函數(shù)

    基本語法

    drop function 函數(shù)名;

    示例:

    drop function my_func1;

    2.5、注意事項(xiàng)

    • 自定義函數(shù)屬于用戶級別,只有當(dāng)前客戶端對應(yīng)的數(shù)據(jù)庫中可以使用

    • 可以在不同數(shù)據(jù)庫下看到函數(shù),但是不可以調(diào)用

    • 自定義函數(shù)通常是為了將多行代碼集合到一起解決一個(gè)重復(fù)性的問題

    4.函數(shù)必須規(guī)范返回值,那么在函數(shù)內(nèi)部不能使用select指令,select一旦執(zhí)行就會的到一個(gè)結(jié)果集 result set;

    可以使用給變量賦值語句

    select 字段 into @變量;

    3、函數(shù)流程結(jié)構(gòu)案例

    需求:

    從1開始,直到用戶傳入的對應(yīng)的值位置,自動求和,凡是5的倍數(shù)都不要

    設(shè)計(jì):

    • 創(chuàng)建函數(shù)

    • 需要一個(gè)形參,確定要累加到什么位置

    • 需要定義一個(gè)變量來保存對應(yīng)的結(jié)果

    • 內(nèi)容部需要一個(gè)循環(huán)來實(shí)現(xiàn)迭代累加

    • 循環(huán)內(nèi)部需要進(jìn)行條件判斷控制,5的倍數(shù)

    定義函數(shù):

    -- 創(chuàng)建一個(gè)自動求和的函數(shù)
    -- 修改語句結(jié)束符
    delimiter $$
    -- 創(chuàng)建函數(shù)
    create function my_sum(end_value int) returns int
    begin
        -- 聲明局部變量
        declare res int default 0;
        declare i int default 0;
        -- 循環(huán)處理
        mywhile: while i <= end_value do
            -- mysql中沒有++
            set i = i + 1; 
            --  判斷當(dāng)前數(shù)據(jù)是否合理
            if i % 5 = 0 then
                iterate mywhile;
            end if;
            -- 修改變量,累加
            set res = res + i;
        end while;
        -- 返回值
        return res;
    end
    
    -- 結(jié)束
    $$
    -- 修改語句結(jié)束符
    delimiter ;

    調(diào)用函數(shù):

    -- 實(shí)參個(gè)數(shù)必須等于形參個(gè)數(shù)
    select my_sum(10);

    以上就是關(guān)于“MySQL內(nèi)置函數(shù)和自定義函數(shù)怎么使用”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI