先來看段mysql查詢文章回復(fù)語句:
代碼如下:
#查詢文章回復(fù)
------------------------------
procedurestructurefor`sp_select_reply_article`
------------------------------
dropprocedureifexists`sp_select_reply_article`;
delimiter;;
createdefiner=`root`@`localhost`procedure`sp_select_reply_article`(in`ra_id`int,in`pagefrom`int,in`pagesize`int)
begin
#routinebodygoeshere...
set@ra_id=ra_id;
set@pagefrom=pagefrom;
set@pagesize=pagesize;
set@ssra=concat('select*fromgk_articlewhereid=?limit?,?');
preparesqlqueryfrom@ssra;
executesqlqueryusing@ra_id,@pagefrom,@pagesize;
end
delimiter;
#技術(shù)點(diǎn)1:mysql5.1不支持limit參數(shù)(mysql5.5就支持了),如果編寫存儲過程時(shí)使用limit做變量,那是需要用動態(tài)sql來構(gòu)建的,而這樣做性能肯定沒有靜態(tài)sql好。主要代碼如下:
代碼如下:
set@ssra=concat('select*fromgk_articlewhereid=?limit?,?');
preparesqlqueryfrom@ssra;
executesqlqueryusing@ra_id,@pagefrom,@pagesize;
#技術(shù)點(diǎn)2:如果同時(shí)需要返回受影響行數(shù)需要在語句后面添加語句:row_count()函數(shù),兩條語句之間需要“;”分隔。
代碼如下:
#更新數(shù)據(jù)
------------------------------
--procedurestructurefor`sp_update_permission`
------------------------------
dropprocedureifexists`sp_update_permission`;
delimiter;;
createdefiner=`root`@`localhost`procedure`sp_update_permission`(in`puser_uid`varchar(20),in`plevel`int,in`ppower`int)
begin
#routinebodygoeshere...
set@puser_uid=puser_uid;
set@plevel=plevel;
set@ppower=ppower;
updategk_permissionset`level`=@plevel,power=@ppowerwhereuser_uid=convert(@puser_uidusingutf8)collateutf8_unicode_ci;
end
delimiter;
#技術(shù)點(diǎn)3:mysql進(jìn)行字符串比較時(shí)發(fā)生錯(cuò)誤(illegalmixofcollations(utf8_unicode_ci,implicit)and(utf8_general_ci,implicit)foroperation'='),解決方法:將比較等式一邊進(jìn)行字符串轉(zhuǎn)換,如改為“convert(b.fullcodeusingutf8)collateutf8_unicode_ci”,主要代碼如下:
代碼如下:
updategk_permissionset`level`=@plevel,power=@ppowerwhereuser_uid=convert(@puser_uidusingutf8)collateutf8_unicode_ci;
代碼如下:
#插入數(shù)據(jù)
------------------------------
--procedurestructurefor`sp_insert_user`
------------------------------
dropprocedureifexists`sp_insert_user`;
delimiter;;
createdefiner=`root`@`localhost`procedure`sp_insert_user`(in`uid`varchar(20),in`upw`varchar(32),in`name`varchar(20),in`sex`int,in`phone`varchar(20),in`u_id`int,in`s_id`int,in`j_id`int)
begin
#routinebodygoeshere...
set@uid=uid;
set@upw=upw;
set@uname=uname;
set@sex=sex;
set@phone=phone;
#由于外鍵約束,所以添加的外鍵字段需要在對應(yīng)外鍵所在表有相應(yīng)數(shù)據(jù)
set@u_id=u_id;
set@s_id=s_id;
set@j_id=j_id;
set@verifytime=date('0000-00-00');
insertintogk_user(uid,upw,uname,sex,phone,u_id,s_id,j_id,verifytime)
values(@uid,@upw,@uname,@sex,@phone,@u_id,@s_id,@j_id,@verifytime);
#查詢結(jié)果會自動返回受影響行數(shù)
end
delimiter;
代碼如下:
#根據(jù)id刪除數(shù)據(jù)
------------------------------
--procedurestructurefor`sp_delete_exchange_by_id`
------------------------------
dropprocedureifexists`sp_delete_exchange_by_id`;
delimiter;;
createdefiner=`root`@`localhost`procedure`sp_delete_exchange_by_id`(in`eid`int)
begin
#routinebodygoeshere...
set@eid=eid;
deletefromgk_exchangewhereid=@eid;
end
delimiter;
代碼如下:
#通過賬號查詢用戶或者管理員
------------------------------
--procedurestructurefor`sp_select_user_by_uid`
------------------------------
dropprocedureifexists`sp_select_user_by_uid`;
delimiter;;
createdefiner=`root`@`localhost`procedure`sp_select_user_by_uid`(in`uid`varchar(20),in`getadmin`int)
begin
#routinebodygoeshere...
set@uid=uid;
#set@getadmin=getadmin;
#查詢管理員
if(getadmin=1)then
selectus.*,un.`name`,se.`name`,jo.`name`,pe.`level`,pe.powerfromgk_userasus,gk_unitasun,gk_sectionasse,gk_jobtitleasjo,gk_permissionaspewhereus.u_id=un.idandus.s_id=se.idandus.j_id=jo.idandus.uid=pe.user_uidandus.uid=convert(@uidusingutf8)collateutf8_unicode_ci;
endif;
#查詢用戶
if(getadmin=0)then
selectus.*,un.`name`,se.`name`,jo.`name`fromgk_userasus,gk_unitasun,gk_sectionasse,gk_jobtitleasjowhereus.u_id=un.idandus.s_id=se.idandus.j_id=jo.idandus.uid=convert(@uidusingutf8)collateutf8_unicode_ci;
endif;
end
delimiter;
#技術(shù)點(diǎn)4:這個(gè)存數(shù)過程需要用到控制語句(ifelseelseifwhilelooprepeatleaveiterate)。
代碼如下:
if(getadmin=1)then
#語句…
endif;
#技術(shù)點(diǎn)5:在傳入?yún)?shù)不匹配的情況下報(bào)錯(cuò)(columncountdoesn'tmatchvaluecountatrow1),這個(gè)就是細(xì)心問題了,詳細(xì)檢查參數(shù)吧。
#技術(shù)點(diǎn)6:獲取當(dāng)前時(shí)間的函數(shù):now()
#技術(shù)點(diǎn)7:“`”這個(gè)符號是反單引號,兩個(gè)反單引號夾起來的會被當(dāng)做變量,一般是在定義字段時(shí)遇到關(guān)鍵字沖突的時(shí)候會用到。