http://blog.csdn.net/pleasurehappy/article/details/51087995
http://www.jb51.net/article/48475.htm
http://www.jb51.net/article/42906.htm
创建表格:
CREATE TABLE `netingcn_proc_test` (<br> `id` INTEGER(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(20), `password` VARCHAR(20), PRIMARY KEY (`id`) )ENGINE=InnoDB;
engine=InnoDB; 这是存储引擎
同时插入多条语句:
insert into netingcn_proc_test(name, password) values
('procedure1', 'pass1'),
('procedure2', 'pass2'),
('procedure3', 'pass3'),
('procedure4', 'pass4');如果是数组中的数据,就遍历成一条上面的语句,直接插入,这样节约资源
mysql中的函数和存储过程的区别在哪里?
函数可以使用return 返回数据,而存储过程不能使用return 其他的全部一样。
创建 函数的代码:把 procedure 改成 function 即可
delimiter $$ drop function if exists `cx` $$ create function cx() begin declare i int; set i=1; while i<10000 DO set i=i+1; end while; return i; end $$
存储过程的语法:
CREATE PROCEDURE test(a INT, OUT b FLOAT, INOUT c INT)
IN为默认类型,值必须在调用时指定,值不能返回(值传递)
OUT值可以返回(指针传递)
INOUT值必须在调用时指定,值可以返回
在命令行中如何声明变量?
set @a=33;
读取变量:@a
select @a; //33
select 3*@a; //99
声明变量:
declare id int(5) declare tmpName varchar(30) #连接字符串 set str= CONCAT(my_string,num,',');
以上这是声明,声明后边的参数和 创建表 写的参数是一样的。
mysql中不能直接赋值,需要使用 set 进行赋值。 set int =5;
每一条语句结束后,需要使用分号进行分割。
mysql中的判断语句:
if(id<5) then code.... end if; if(id<5) then code... elseif(id<10) then code2.... else code3... end if;
if date_day is null then set date_day = date_add(now(),interval -1 day); end if;
好的,现在看看case是如何使用的:
case varName when "1" then code... when "2" then code2... when "3" then code3... end case;
mysql中如何循环?
循环有三种:
WHILE expression DO Statements; END WHILE; #这也是一种循环 REPEAT ... UNTIL condition END REPEAT; delimiter $$ drop procedure if exists `cx` $$ create procedure cx() begin declare flag int default 0; declare i int; set i=1; REPEAT insert into t(title)values(i); set i=i+1; if(i<10000) then set flag=1; end if; UNTIL flag=1 END REPEAT; end $$ LOOP....END LOOP 或者 [loop_label]:LOOP code... END LOOP [loop_label] 使用if语句进行判断 离开这个循环 LEAVE [loop_label] delimiter $$ drop procedure if exists `cx` $$ create procedure cx() begin declare i int; set i=1; lp1:LOOP insert into t(title)values(i); set i=i+1; if(i<10000) then LEAVE lp1; end if; end LOOP lp1; end $$ while ... end while; delimiter $$ drop procedure if exists `cx` $$ create procedure cx() begin declare i int; set i=1; while i<10000 DO insert into t(title)values(i); set i=i+1; end while; end $$
如何查看是怎么创建的这个存储过程?
show create procedure c42 \G
查看 c42这个存储过程是怎么创建的,会显示出整个创建的代码。
查看此数据中创建所有的存储过程:
show procedure status \G
删除一个存储过程:
drop procedure if exists `c45` ;
如果c45这个存储过程存在,就删除
下面这段代码使用游标:
声明游标
DECLARE cur1 CURSOR FOR select id,title,brands from web_article where nav=42;
打开游标
OPEN cur1
遍历游标
WHILE(tmpflat is not null)DO
set sBrands=0;
if(locate("米其林",tmpTitle)>0) then
set sBrands=15;
elseif(locate("普利司通",tmpTitle)>0) then
set sBrands=16;
end if;
update web_article set brands=sBrands where id=tmpId;
if(locate("轿车",tmpTitle)>0) then
update web_article set tyrenet_type=1 where id=tmpId;
elseif(locate("卡客车",tmpTitle)>0) then
update web_article set brands=3 where id=tmpId;
end if;
FETCH cur1 INTO tmpId,tmpTitle,tmpBrands;
END WHILE;关闭游标
CLOSE cur1;
这段代码的意思:读取表中的数据,然后遍历 判断是否属于此分类,并update 更新brands 这个参数。
delimiter $$
drop procedure if exists `c42` $$
create procedure c42()
begin
DECLARE tmpflat INT DEFAULT 0;
DECLARE sBrands varchar(30) default "0";
DECLARE tmpId int(4);
DECLARE tmpTitle varchar(80);
DECLARE tmpBrands varchar(80);
DECLARE cur1 CURSOR FOR select id,title,brands from web_article where nav=42;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET tmpflat = null;
OPEN cur1;
FETCH cur1 INTO tmpId,tmpTitle,tmpBrands;
WHILE(tmpflat is not null)DO
set sBrands=0;
if(locate("米其林",tmpTitle)>0) then
set sBrands=15;
elseif(locate("普利司通",tmpTitle)>0) then
set sBrands=16;
end if;
update web_article set brands=sBrands where id=tmpId;
if(locate("轿车",tmpTitle)>0) then
update web_article set tyrenet_type=1 where id=tmpId;
elseif(locate("卡客车",tmpTitle)>0) then
update web_article set brands=3 where id=tmpId;
end if;
FETCH cur1 INTO tmpId,tmpTitle,tmpBrands;
END WHILE;
CLOSE cur1;
end $$
delimiter ;存错过程中做的是事情就是一组语句,写在一起,调用的时候,执行以下即可,
比如说,更新 ,插入 等操作
看看下面的代码:
IF NOT _done THEN IF total > 0 THEN SELECT ordUID into uid FROM fightorder WHERE fightorder.alipayNo=parentId limit 1;#查询用户UID #INSERT INTO test values(total,uid); UPDATE sysusers SET integral=integral+total WHERE sysusers.UID=uid;#新增积分 END IF; END IF;