日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql 自定义函数

發(fā)布時間:2024/6/18 数据库 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 自定义函数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

新建:

Create function function_name(參數(shù)列表)returns返回值類型

函數(shù)體

函數(shù)名,應(yīng)該合法的標識符,并且不應(yīng)該與已有的關(guān)鍵字沖突。

一個函數(shù)應(yīng)該屬于某個數(shù)據(jù)庫,可以使用db_name.funciton_name的形式執(zhí)行當前函數(shù)所屬數(shù)據(jù)庫,否則為當前數(shù)據(jù)庫。

參數(shù)部分,由參數(shù)名和參數(shù)類型組成。

返回值類類型

函數(shù)體由多條可用的mysql語句,流程控制,變量聲明等語句構(gòu)成。

多條語句應(yīng)該使用begin end語句塊包含。

注意,一定要有return返回值語句。

刪除:

Dropfunction if existsfunction_name;

查看:

Show function status like ‘partten’

Show create functionfunction_name;

修改:

Alter functionfunction_name函數(shù)選項。

例子:

Hello world!

IF語句

IF search_conditionTHEN

statement_list

[ELSEIF search_conditionTHENstatement_list]

...

[ELSE statement_list]ENDIF;

CASE語句

CASE case_value

WHEN when_valueTHENstatement_list

[WHEN when_value THENstatement_list]

...

[ELSE statement_list]

END CASE;?

循環(huán):

While

[begin_label:]WHILEsearch_conditionDO

statement_list

END WHILE [end_label];

如果需要在循環(huán)內(nèi)提前終止 while循環(huán),則需要使用標簽;標簽需要成對出現(xiàn)。

退出循環(huán)

退出整個循環(huán)leave 相當于break

退出當前循環(huán)iterate 相當于 continue

通過退出的標簽決定退出哪個循環(huán)。

變量聲明:
語法:
DECLARE var_name[,...] type [DEFAULT value]
這個語句被用來聲明局部變量。要給變量提供一個默認值,請包含一個DEFAULT子句。值可以被指定為一個表達式,不需要為一個常數(shù)。如果沒有DEFAULT子句,初始值為NULL。
使用
語序使用 set 和 select into語句為變量賦值。

注意在函數(shù)內(nèi)是可以使用全局變量(用戶自定義的變量的)@XXX 全局變量不用聲明 可以直接@XXX使用。

例子:獲取當前班級內(nèi),最大的學號。?

參考學生表?

create table join_student(?

stu_id int not null auto_increment,?

stu_no char(10),?

class_id int not null,?

stu_name varchar(10),?

stu_info text,?

primary key (stu_id)?

);?

計算新增學號?

drop function if existssno;

?delimiter $$ #在包含有語句塊時 可以更換語句結(jié)束符“;” 為“$$”?

create function sno(c_id int)returns char(10)?

begin?

declare last_no char(10); #聲明一個局部變量 用來保存當前最大的學號, 如果沒有就為null

declare class_name char(10);?

select stu_no from join_student where class_id=c_id order by stu_no desc limit 1 into last_no;?

if last_no is null then #如果為空代表當前班級沒有學生 從1開始,獲得班級名字?

return concat ((select c_name from join_class where id=c_id into class_name),'001'); #concat() 函數(shù)的作用是連接字符串。?

else?

return concat(left(last_no,7),lpad(right(last_no,3) + 1, 3, '0'));?

end if;

?#return @last_no;?

end?

$$

delimiter ;?

隨機獲得學生名字。?

drop function if exists sname;?

delimiter $$?

create function sname() returns char(2)?

begin?

declare first_name char(16) default '趙錢孫李周吳鄭王馮陳褚衛(wèi)蔣沈韓楊';?

declare last_name char(10) default '甲乙丙丁戊己庚辛壬癸';?

declare full_name char(2);?

set full_name=concat(substring(first_name,floor(rand()*16+1), 1), substring(last_name,floor(rand()*10+1), 1));?

return full_name;?

end?

$$?

delimiter ;

========================================================================================

mysql常用內(nèi)置函數(shù)

數(shù)值函數(shù)

Abs(X),絕對值abs(-10.9) = 10

Format(X,D),格式化千分位數(shù)值format(1234567.456, 2) =1,234,567.46

Ceil(X),向上取整ceil(10.1) = 11

Floor(X),向下取整floor (10.1) = 10

Round(X),四舍五入去整

Mod(M,N) M%N M MOD N 求余 10%3=1

Pi(),獲得圓周率

Pow(M,N) M^N

Sqrt(X),算術(shù)平方根

Rand(),隨機數(shù)

TRUNCATE(X,D) 截取D位小數(shù)

時間日期函數(shù)

Now(),current_timestamp(); 當前日期時間

Current_date();當前日期

current_time();當前時間

Date(‘yyyy-mm-dd HH;ii:ss’);獲取日期部分

Time(‘yyyy-mm-dd HH;ii:ss’);獲取時間部分

Date_format(‘yyyy-mm-dd HH;ii:ss’,’%D %y %a %d %m %b %j');

Unix_timestamp();獲得unix時間戳

From_unixtime();//從時間戳獲得時間

字符串函數(shù)

LENGTH(string ) //string長度,字節(jié)

CHAR_LENGTH(string) //string的字符個數(shù)

SUBSTRING(str ,position [,length ]) //從str的position開始,取length個字符

REPLACE(str ,search_str ,replace_str) //在str中用replace_str替換search_str

INSTR(string ,substring ) //返回substring首次在string中出現(xiàn)的位置

CONCAT(string [,... ]) //連接字串

CHARSET(str) //返回字串字符集

LCASE(string ) //轉(zhuǎn)換成小寫

LEFT(string ,length ) //從string2中的左邊起取length個字符

LOAD_FILE(file_name) //從文件讀取內(nèi)容

LOCATE(substring , string [,start_position ]) //同INSTR,但可指定開始位置

LPAD(string ,length ,pad ) //重復(fù)用pad加在string開頭,直到字串長度為length

LTRIM(string ) //去除前端空格

REPEAT(string ,count ) //重復(fù)count次

RPAD(string ,length ,pad) //在str后用pad補充,直到長度為length

RTRIM(string ) //去除后端空格

STRCMP(string1 ,string2 ) //逐字符比較兩字串大小

流程函數(shù):

CASE WHEN [condition]THEN result[WHEN [condition]THEN result ...][ELSE result]END 多分支

IF(expr1,expr2,expr3) 雙分支。

聚合函數(shù)

Count()

Sum();

Max();

Min();

Avg();

Group_concat()

其他常用函數(shù)

Md5();

Default();

轉(zhuǎn)載于:https://www.cnblogs.com/ljcphper/p/4387645.html

總結(jié)

以上是生活随笔為你收集整理的mysql 自定义函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。