您好,登錄后才能下訂單哦!
- 創(chuàng)建 -- SQL的函數(shù)必須有返回值 -- create function 函數(shù)名稱(參數(shù)列表) returns 返回類型 create function f1() returns int return 10; - 查看 -- 查看 mysql.proc select db,name,type from mysql.proc; - 調(diào)用 -- 調(diào)用 select f1(); - 刪除 -- 刪除函數(shù) drop function f1; delete from mysql.proc where name = 'xx'; - 變量 -- 臨時修改 delimiter // create function f1(a int,b int) returns int begin -- 定義一個變量 res declare res int default 0; -- 將 a + b的結(jié)果賦值給 res set res = a + b; -- 返回 res return res; end // delimiter ; -- 用戶變量的注意點 -- 在 sql 語句中給用戶變量賦值的時候 不能夠使用 '=' select count(*) from mm_users; select @count = count(*) from mm_users; -- '=' 表示判斷是否相等 - 判斷 判斷 if 條件1 then 語句1; elseif 條件2 then 語句2; else 語句 end if; - 循環(huán) 循環(huán) while 條件 do 語句; end while; 退出循環(huán):leave,相當(dāng)于break 退出本次循環(huán):iterate,相當(dāng)于continue - 示例 delimiter // create function f3() returns varchar(300) begin -- 1. 聲明遞增的值 i, 拼接結(jié)果的 res declare i int default 0; declare res varchar(300) default ''; -- 2. 通過 while 遞增 i while i <= 100 do -- 3. 判斷 i 是否是偶數(shù) if i % 2 = 0 then -- 4. 拼接偶數(shù) set res = concat(res,' ',i); end if; -- 遞增 i set i = i + 1; end while; -- 5. 返回 res return res; end // delimiter ;
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。