2)case语句
mysql > DELIMITER //
mysql >CREATEPROCEDUREproc3 (inparameterint)
->begin
->declarevarint;
->setvar=parameter+1;
->casevar
->when0then
->insertintotvalues(17);
->when1then
->insertintotvalues(18);
->else
->insertintotvalues(19);
->endcase;
->end;
-> //
mysql > DELIMITER ;
4.6.2循环语句
1)while ···· end whilemysql > DELIMITER //
mysql >CREATEPROCEDUREproc4()
->begin
->declarevarint;
->setvar=0;
-> while var<6 do
->insertintotvalues(var);
->setvar=var+1;
->endwhile;
->end;
-> //
mysql > DELIMITER ;
2)repeat···· end repeat:它在执行操作后检查结果,而while则是执行前进行检查。mysql > DELIMITER //
mysql >CREATEPROCEDUREproc5 ()
->begin
->declarevint;
->setv=0;
-> repeat
->insertintotvalues(v);
->setv=v+1;
-> until v>=5
->endrepeat;
->end;
-> //
mysql > DELIMITER ;
3)loop ·····end loop:loop循环不需要初始条件,这点和while循环相似,同时和repeat循环一样不需要结束条件, leave语句的意义是离开循环。
[begin_label:]LOOP
statement_list
END LOOP [end_label]
mysql > DELIMITER //
mysql >CREATEPROCEDUREproc6 ()
->begin
->declarevint;
->setv=0;
-> LOOP_LABLE:loop
->insertintotvalues(v);
->setv=v+1;
-> if v >=5then
-> leave LOOP_LABLE;
->endif;
->endloop;
->end;
-> //
mysql > DELIMITER ;
4.6.3LEAVE语句:跳出循环控制
LEAVE label
注:label标号可以用在begin repeat while 或者loop 语句前,语句标号只能在合法的语句前面使用。可以跳出循环,使运行指令达到复合语句的最后一步。
4.6.4ITERATE迭代:通过引用复合语句的标号,跳出本次循环,执行下一轮。mysql > DELIMITER //
mysql >CREATEPROCEDUREproc10 ()
->begin
->declarevint;
->setv=0;
-> LOOP_LABLE:loop
-> if v=3then
->setv=v+1;
-> ITERATELOOP_LABLE;
->endif;
->insertintotvalues(v);
->setv=v+1;
-> if v>=5then
-> leave LOOP_LABLE;
->endif;
->endloop;
->end;
-> //
mysql > DELIMITER ;
4.7MySQL存储过程的基本函数
(1).字符串类
CHARSET(str) //返回字串字符集CONCAT (string2 [,... ]) //连接字串INSTR (string ,substring ) //返回substring首次在string中出现的位置,不存在返回0
LCASE (string2 ) //LCASE (string2 ) //转换成小写LEFT (string2 ,length ) //从string2中的左边起取length个字符LENGTH (string ) //string长度LOAD_FILE (file_name ) //从文件读取内容LOCATE (substring , string [,start_position ] )同INSTR,但可指定开始位置LPAD (string2 ,length ,pad ) //重复用pad加在string开头,直到字串长度为length
LTRIM (string2 ) //LTRIM (string2 ) //去除前端空格REPEAT (string2 ,count ) //重复count次REPLACE (str ,search_str ,replace_str ) //在str中用replace_str替换search_str
RPAD (string2 ,length ,pad) //RPAD (string2 ,length ,pad) //在str后用pad补充,直到长度为length
RTRIM (string2 ) //RTRIM (string2 ) //去除后端空格STRCMP (string1 ,string2 ) //逐字符比较两字串大小,SUBSTRING (str , position [,length ]) //从str的position开始,取length个字符,注:mysql中处理字符串时,默认第一个字符下标为1,即参数position必须大于等于1例:
mysql>selectsubstring('abcd',0,2);
+-----------------------+
|substring('abcd',0,2) |
+-----------------------+
| |
+-----------------------+
1 rowinset(0.00 sec)
mysql>selectsubstring('abcd',1,2);
+-----------------------+
|substring('abcd',1,2) |
+-----------------------+
| ab |
+-----------------------+
1 rowinset(0.02 sec)
TRIM([[BOTH|LEADING|TRAILING] [padding] FROM]string2) //去除指定位置的指定字符UCASE (string2 ) //转换成大写RIGHT(string2,length) //取string2最后length个字符SPACE(count) //生成count个空格
(2).数学类
ABS (number2 ) //绝对值BIN (decimal_number ) //十进制转二进制CEILING (number2 ) //向上取整CONV(number2,from_base,to_base) //进制转换FLOOR (number2 ) //向下取整FORMAT (number,decimal_places ) //保留小数位数HEX (DecimalNumber ) //转十六进制注:HEX()中可传入字符串,则返回其ASC-11码,如HEX('DEF')返回4142143也可以传入十进制整数,返回其十六进制编码,如HEX(25)返回19
LEAST (number , number2 [,..]) //LEAST (number , number2 [,..]) //求最小值MOD (numerator ,denominator ) //求余POWER (number ,power ) //求指数RAND([seed]) //随机数
ROUND (number [,decimals ]) //四舍五入,decimals为小数位数]
注:返回类型并非均为整数,如:
(1)默认变为整形值mysql>selectround(1.23);
+-------------+
| round(1.23) |
+-------------+
| 1 |
+-------------+
1 rowinset(0.00 sec)
mysql>selectround(1.56);
+-------------+
| round(1.56) |
+-------------+
| 2 |
+-------------+
1 rowinset(0.00 sec)
(2)可以设定小数位数,返回浮点型数据mysql>selectround(1.567,2);
+----------------+
| round(1.567,2) |
+----------------+
| 1.57 |
+----------------+
1 rowinset(0.00 sec)
(3).日期时间类ADDTIME (date2 ,time_interval ) //将time_interval加到date2
CONVERT_TZ (datetime2 ,fromTZ ,toTZ ) //转换时区CURRENT_DATE ( ) //当前日期CURRENT_TIME ( ) //当前时间CURRENT_TIMESTAMP ( ) //当前时间戳DATE (datetime ) //返回datetime的日期部分DATE_ADD (date2 , INTERVAL d_value d_type ) //在date2中加上日期或时间DATE_FORMAT (datetime ,FormatCodes ) //使用formatcodes格式显示datetime
DATE_SUB (date2 , INTERVAL d_value d_type ) //在date2上减去一个时间DATEDIFF (date1 ,date2 ) //两个日期差DAY (date ) //返回日期的天DAYNAME (date ) //英文星期DAYOFWEEK (date ) //星期(1-7) ,1为星期天DAYOFYEAR (date ) //一年中的第几天EXTRACT (interval_name FROM date ) //从date中提取日期的指定部分MAKEDATE (year ,day ) //给出年及年中的第几天,生成日期串MAKETIME (hour ,minute ,second ) //生成时间串MONTHNAME (date ) //英文月份名NOW ( ) //当前时间SEC_TO_TIME (seconds ) //秒数转成时间STR_TO_DATE (string ,format ) //字串转成时间,以format格式显示TIMEDIFF (datetime1 ,datetime2 ) //两个时间差TIME_TO_SEC (time ) //时间转秒数]
WEEK (date_time [,start_of_week ]) //第几周YEAR (datetime ) //年份DAYOFMONTH(datetime) //月的第几天HOUR(datetime) //小时LAST_DAY(date) //date的月的最后日期MICROSECOND(datetime) //微秒MONTH(datetime) //月MINUTE(datetime) //分返回符号,正负或0
SQRT(number2) //开平方